简体   繁体   中英

no route for route that is shown in 'rake routes'

I added an extra route in my rails application to my nested resources like this.

 resources :questions do 
    resources :answers do 

       match "/bestanswer", :to => "answers#bestanswer", :via => :post

      end
  end 

Running 'rake routes' shows the following path

question_answer_bestanswer POST   /questions/:question_id/answers/:answer_id/bestanswer(.:format) answers#bestanswer

I tried to use the path in a form (which is repeated many times for each answer) like this

<%= 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 %> 

However, when I go to the page where this form is displayed, I get this error

No route matches {:controller=>"answers", :action=>"bestanswer"}

Can you explain what I've done wrong?

You are missing the route parameters, your form should be:

<%= form_tag question_answer_bestanswer_path( answer.question, answer ), method: :post do%>
   <%= submit_tag "Accept this answer as the best answer", :class => 'btn ' %>
<% end %>

And the hidden_field_tag 's are not necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM