我有三种模式:帖子,评论和问题,我试图将帖子与问题联系起来。
我的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
答案 0 :(得分:1)
首先删除.select(:body)
。这使得它返回body属性,但不能使您正确使用该对象。
然后尝试:
link_to comment.body, comment_questions_path( comment )
答案 1 :(得分:0)
在您的终端中运行:
$ rake routes