![](/img/trans.png)
[英]Nested Form - I cannot get all of the attributes params to pass through to my strong params
[英]My nested attributes are not passing through the strong Params
我有一個交貨單的表格,其中包含一個用餐表格。 一頓飯是由物品組成的,物品也是對象。 交貨表格看起來像這樣......
<%= form_for @delivery do | f | %>
<%= f.label :address %>
<f.text_field :address $>
<% if @meal != nil %>
<% meal = @meal %>
<% else %>
<% meal = Meal.new %>
<% end %>
<%= f.fields_for meal %>
<%= render partial: "meals/form", locals: {selected_meal: meal} %>
<% end %>
<br>
<%= f.submit "Continue" %>
<% end %>
這頓飯的形式看起來像這樣
<label>Meal Name: (Optional) </label>
<%= f.text_field :name %>
<br>
<h4>-----------Pizzas------------</h4>
<%= f.collection_check_boxes :meal_item, Item.pizza_items, :id, :show %>
<br>
<h4>-------Cold Sandwiches-------</h4>
<%= f.collection_check_boxes :meal_item, Item.cold_items, :id, :show %>
<br>
<h4>-------Hot Sandwiches-------</h4>
<%= f.collection_check_boxes :meal_item, Item.hot_items, :id, :show %>
<br>
<h4>-----------Salads-----------</h4>
<%= f.collection_check_boxes :meal_item, Item.salad_items, :id, :show %>
<br>
<h4>------------Pastas------------</h4>
<%= f.collection_check_boxes :meal_item, Item.hot_items, :id, :show %>
<br>
<h4>-----------Chicken-----------</h4>
<%= f.collection_check_boxes :meal_item, Item.hot_items, :id, :show %>
<br>
<%= f.submit "Continue" %>
<% end %>
當帶有嵌套餐單的delivery form通過后,進入delivery#confirm動作,加上strong param,看起來是這樣的……
def confirm
binding.pry
@delivery = Delivery.new()
if (SessionHelpers.is_logged_in?(session))
@credit = SessionHelpers.current_user(session).credit
end
end
private
def delivery_params
params.require(:delivery).permit(:address, :order_user_id, :total_price, :delivered, meal_attributes: [:name, items:[]])
end
每當表單傳遞時, delivery_params
只傳遞了地址,沒有通過 go 的餐點屬性,但它們存在於常規參數中。 我怎樣才能解決這個問題?
可能來自你如何稱呼你的嵌套形式。 也許嘗試類似:
<%= f.fields_for :meal do |meal_f| %>
在你的部分:
<%= meal_f.text_field :name %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.