我在rails应用程序中添加了一条额外的路由到这样的嵌套资源。
resources :questions do
resources :answers do
match "/bestanswer", :to => "answers#bestanswer", :via => :post
end
end
运行'rake routes'会显示以下路径
question_answer_bestanswer POST /questions/:question_id/answers/:answer_id/bestanswer(.:format) answers#bestanswer
我尝试使用表单中的路径(每个答案重复多次),如此
<%= form_tag question_answer_bestanswer_path, method: :post do%>
<%= hidden_field_tag :answer_id, answer.id %>
<%= hidden_field_tag :question_id, answer.question.id %>
<%= submit_tag "Accept this answer as the best answer", :class => 'btn ' %>
<% end %>
但是,当我转到显示此表单的页面时,我收到此错误
No route matches {:controller=>"answers", :action=>"bestanswer"}
你能解释一下我做错了吗?
答案 0 :(得分:1)
您缺少路线参数,您的表格应为:
<%= form_tag question_answer_bestanswer_path( answer.question, answer ), method: :post do%>
<%= submit_tag "Accept this answer as the best answer", :class => 'btn ' %>
<% end %>
并且hidden_field_tag
不是必需的。