0

我有三个模型:帖子、评论和问题,并试图从帖子链接到问题。

我的 routes.rb 文件如下所示:

resources :posts do
 resources :comments do
end
end

resources :comments do
 resources :questions do
 end
end

这是帖子视图文件:

<% post.comments.select(:body).order('created_at desc').limit(2).each do |comment| %>
<%= link_to (comment.body), comment_questions_path(#what to pass in here?) %>
<% end %>

我努力了:

<%= link_to (comment.body), comment_questions_path(comment, @question) %>

错误:

No route matches {:controller=>"questions", :action=>"index", :comment_id=>#<Comment id: nil, body: "Describe what it was like to witness the explosion?...">, :format=>nil} missing required keys: [:comment_id]    <%= link_to (comment.body), comment_questions_path(#what to pass in here?) %>

和:

 <%= link_to (comment.body), comment_questions_path(:comment_id, question) %>

错误:

 Couldn't find Comment with id=comment_id

和:

<%= link_to (comment.body), comment_questions_path(:comment_id, :id) >

错误:

 Couldn't find Comment with id=comment_id

这是路线:

comment_questions GET    /comments/:comment_id/questions(.:format)            questions#index

谢谢您的帮助!和问题控制器中的索引功能:

def index
@comment = Comment.find params[:comment_id]
@questions = @comment.questions
end    
4

2 回答 2

1

首先放下.select(:body). 这使它返回 body 属性,但不能使您正确使用该对象。

然后尝试:

link_to comment.body, comment_questions_path( comment )
于 2013-10-26T18:53:35.440 回答
0

Run this in your terminal:

$ rake routes
于 2013-10-26T18:32:55.843 回答