[英]Nested resources link_to
sI'm having trouble linking to something like example.com/r/leagueoflegends/posts/1 . 我无法链接到example.com/r/leagueoflegends/posts/1之类的东西。 In my link i have 在我的链接中
<%= link_to posts.title, category_post_path %>
but end up getting "No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:category_id, :id]". 但最终得到“没有路线匹配{:action =>“ show”,:controller =>“ posts”}缺少必需的键:[:category_id,:id]”。 I've tried other ways but still cant get it to work right. 我尝试了其他方法,但仍然无法正常工作。
My routes look like this 我的路线看起来像这样
resources :categories, path: 'r', except: [:index] do
resources :posts
end
Home controller 家庭控制器
def index
@posts = Post.all
end
You should indicate to which category you're linking (not only post), so you should have: 您应指明要链接到的类别(不仅是帖子),因此应具有:
<%= link_to post.title, [@category, post] %>
of course, you should have @category
variable set, which should be in before filter in your controller: 当然,您应该设置@category
变量,该变量应该在控制器中的过滤器之前:
before_filter :set_category
# ...
private
def set_category
@category = Category.find(params[:category_id])
end
and in your index action: 并在您的索引操作中:
def index
@posts = @category.posts
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.