2013-10-26 4 views
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  

ответ

1

Сначала отбросьте .select(:body). Это позволяет вернуть атрибут body, но не позволяет вам правильно работать с объектом.

Тогда попробуйте:

link_to comment.body, comment_questions_path(comment) 
+0

Это сработало, спасибо! – user2759575

0

Выполнить это в терминале:

$ rake routes 
+0

добавил к этому вопросу. – user2759575

Смежные вопросы