开发者

Rails fields_for not passing all params

开发者 https://www.devze.com 2023-04-12 18:38 出处:网络
I have a form that updates two differe开发者_如何学编程nt models using fields_for. The data for the elements inside the fields_for are getting submitted but the ones for the original form_for are miss

I have a form that updates two differe开发者_如何学编程nt models using fields_for. The data for the elements inside the fields_for are getting submitted but the ones for the original form_for are missing

Here is the form:

<%= form_for @cart, :remote => true do |cart| %>
 <%= fields_for @cart.order, :remote => true do |order| %>
   <%= order.select :country, options_for_select(@country_options) %>
   <%= order.text_field :zip %>
 <% end %>
 <%= cart.select :shipping_method,options_for_select(@shipping_options) %>
<% end %>

Here is what is contained in the params in the update action:

{"_method"=>"put", "utf8"=>"\342\234\223", "action"=>"update", "order"=>{"zip"=>"48360", "country"=>"US"}, "id"=>"1", "controller"=>"carts"}

Why is the shipping_method field not appearing in the params?

Here is the generated HTML form:

<form accept-charset="UTF-8" action="/carts/3" class="edit_cart" data-remote="true" id="edit_cart_3" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" value="✓" type="hidden"><input name="_method" value="put" type="hidden"></div>
  <select id="order_country" name="order[country]"><option value="US">United States</option></select>
  <input id="order_zip" name="order[zip]" size="30" value="90001" type="text">
  <select id="cart_shipping_method" name="cart[shipping_method]"><option value="FedEx Ground" selected="selected">FedEx Ground: $5.00</option></select>
</form>    


I think you should maybe try some thing like this:

<%= cart.select :shipping_method, options_from_collection_for_select(@shipping_options, 'id', 'name') %>

Where 'name' is the parameter you want to display. Otherwise I would suggest inspecting the html of the form. If the select items don't have a value attribute, something is wrong with your erb

options_for_select only work for 2xn arrays


Ok you then I see something else that may cause problems, it think it should be:

<%= cart.fields_for @cart.order, :remote => true do |order| %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号