MiH
MiH

Reputation: 421

Form_for in an index rails 4

My project is about an online Shopping site.Now i'm tring to create Basket. I have an error when i try to edit Basket. My code is :

<% @basket.each do |t|%>
<tr>
<td>
    <%= form_for [:editnum,t], url: user_basket_path(current_user,t) do |f| %>
        <%= f.label :Number%>
        <%= f.number_field :num %>
        <%= f.submit :Edit %>
    <%end%>
</td>

And in the Controller I try to get the params:

params.require(:editnum).permit(:num)

But the error is :

param is missing or the value is empty: editnum

Upvotes: 1

Views: 451

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

It should be...

params.require(:basket).permit(:num)

The error is from the params hash, stating that editnum does not exist. Your params method is requiring the editnum value, hence you should be passing the following params hash:

params: {
   "editnum" => {
      "num" => "value"
   }
}

The strong params method will essentially "require" editnum, and if it doesn't get it, will reject the request & raise an exception (your error).

The way around this is to make sure you're passing the correct values.

Since you're using form_for, you should expect the "top level" params hash to be the object you're editing. In your case, it is a basket, so I would expect a basket value to be present:

params: {
   "basket" => {
      "num" => "value"
   }
}

--

I think you're getting confused with how you're calling nested data. Just because you're nesting your routes / resources under editnum doesn't mean your data structure will have to conform to it.

Upvotes: 1

Related Questions