gpt4 book ai didi

ruby-on-rails - RoR : nested namespace routes, 未定义方法错误

转载 作者:数据小太阳 更新时间:2023-10-29 07:22:18 26 4
gpt4 key购买 nike

我正在开发一个新的 Rails 应用程序的管理部分,我正在尝试设置一些路线以“正确”地做事。我有以下 Controller :

class Admin::BlogsController < ApplicationController
def index
@blogs = Blog.find(:all)
end

def show
@blog = Blog.find(params[:id])
end

...
end

在 routes.rb 中:

map.namespace :admin do |admin|
admin.resources :blogs
end

在 views/admin/blogs/index.html.erb 中:

<% for blog in @blogs %>
<%= link_to 'Delete', admin_blog(blog), :method => :delete
<% end %>

我已验证路由存在:

admin_blogs GET /admin/blogs {:action => "index", :controller=>"admin/blogs"}
admin_blog GET /admin/blogs/:id {:action => "show", :controller => "admin/blogs"}
....

但是当我尝试查看 http://localhost:3000/admin/blogs我得到这个错误:

undefined method 'admin_blog' for #<ActionView::Base:0xb7213da8>

我哪里错了,为什么?

最佳答案

您的删除链接应以 _path 结尾:

<%= link_to 'Delete', admin_blog_path(blog), :method => :delete %>

关于ruby-on-rails - RoR : nested namespace routes, 未定义方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/153183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com