0

I am using rails 3.2.8 and I am having an issue with form_for:

I have the following form within the view of a different model:

<%= form_for pair, { :method => :put } do |f| %>
     <td><%= pair.token.value %></td>
     <td><%= pair.pair_token.value %></td>
     <td><%= pair.freq %></td>
     <td><%= pair.distance %></td>
     <td><%= f.select :distance, [['', nil], ['Strongly Opposite', -3], ['Moderately Opposite', -2], ['Weakly Opposite', -1],
                              ['No Relationship', 0], ['Weakly Similar', 1], ['Moderately Similar', 2], ['Strongly Similar', 3]], {}, {} %></td>
     <td><%= pair.agree %></td>
     <td><%= f.select :agree, [['', nil],['True', 1], ['False', 0]], {}, {} %></td>
     <td><%= f.submit %></td>
<% end %>

This form is within the view for a model called tokens. The page is actually the tokens show.html. I am getting the response:

No route matches [POST] "/pairs/269671"

I am not sure what is going wrong as I am asking for the PUT action and the pair object is trying to do a post.

Thanks

GTDev
  • 5,488
  • 9
  • 49
  • 84

4 Answers4

0

What does your routes file look like?

Rails has default HTTP verbs that it uses for different actions. See here: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

Create happens via POST, and update happens via PUT. So, your routes need to support both, which is what Rails does in the background via resources :pairs in config/routes.rb.

You shouldn't need to, if you're using default Rails conventions, override which HTTP verb is used for a given form submission.

And, if you want further insight into these issues, take a look at this SO question: PUT vs POST in REST

Community
  • 1
  • 1
Carlos Drew
  • 1,633
  • 9
  • 17
0

try this out

<%= form_for pair do |f| %>
     <td><%= pair.token.value %></td>
     <td><%= pair.pair_token.value %></td>
     <td><%= pair.freq %></td>
     <td><%= pair.distance %></td>
     <td><%= f.select :distance, [['', nil], ['Strongly Opposite', -3], ['Moderately Opposite', -2], ['Weakly Opposite', -1],
                              ['No Relationship', 0], ['Weakly Similar', 1], ['Moderately Similar', 2], ['Strongly Similar', 3]], {}, {} %></td>
     <td><%= pair.agree %></td>
     <td><%= f.select :agree, [['', nil],['True', 1], ['False', 0]], {}, {} %></td>
     <td><%= f.submit %></td>
<% end %>

form_for knows how to handle create/update of the record, so there is no need to specify method.

Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
0

Maybe you need to include the token in the form like:

<%= form_for [:tokens, pair] do |f| %>
 <td><%= pair.token.value %></td>
 <td><%= pair.pair_token.value %></td>
 <td><%= pair.freq %></td>
 <td><%= pair.distance %></td>
 <td><%= f.select :distance, [['', nil], ['Strongly Opposite', -3], ['Moderately Opposite', -2], ['Weakly Opposite', -1],
                          ['No Relationship', 0], ['Weakly Similar', 1], ['Moderately Similar', 2], ['Strongly Similar', 3]], {}, {} %></td>
 <td><%= pair.agree %></td>
 <td><%= f.select :agree, [['', nil],['True', 1], ['False', 0]], {}, {} %></td>
 <td><%= f.submit %></td>
<% end %>

So your routes will be:

[POST] /tokens/:token_id/pairs/:pair_id

If that doesn't work try this one:

<%= form_for [@token, pair] do |f| %>
 <td><%= pair.token.value %></td>
 <td><%= pair.pair_token.value %></td>
 <td><%= pair.freq %></td>
 <td><%= pair.distance %></td>
 <td><%= f.select :distance, [['', nil], ['Strongly Opposite', -3], ['Moderately Opposite', -2], ['Weakly Opposite', -1],
                          ['No Relationship', 0], ['Weakly Similar', 1], ['Moderately Similar', 2], ['Strongly Similar', 3]], {}, {} %></td>
 <td><%= pair.agree %></td>
 <td><%= f.select :agree, [['', nil],['True', 1], ['False', 0]], {}, {} %></td>
 <td><%= f.submit %></td>
<% end %>
Raja Hafify
  • 29
  • 1
  • 2
0

Just don't hard code method cause same form is being used in different actions and you are breaking Rails conventions.

@object = MyModel.new
<%= form_for @object do |f| %>

same if you doing edit action

@object = MyModel.find(2)
<%= form_for @object do |f| %>

rails will take care about method. Hope it helps !

user12733
  • 153
  • 1
  • 1
  • 11