BrainLikeADullPencil
BrainLikeADullPencil

Reputation: 11663

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?

Upvotes: 2

Views: 86

Answers (1)

Maur&#237;cio Linhares
Maur&#237;cio Linhares

Reputation: 40333

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.

Upvotes: 1

Related Questions