user15217186
user15217186

Reputation:

My nested attributes are not passing through the strong Params

I have a form for a delivery order that contains a form for a meal inside of it. A meal is made up of items, which are also objects. The form for a delivery looks as so...

<%= 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 %>

and the form for the meal looks as so

  <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 %>

When the delivery form with the nested meal form is passed, it goes to the delivery # confirm action, and with the strong param, it looks like this...

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

Whenever the form is passed, the delivery_params only has the address passed, none of the meal attributes go through, yet they exist in the regular params. How can I fix this?

Upvotes: 0

Views: 266

Answers (1)

Maxence
Maxence

Reputation: 2339

Probably coming from how you call your nested form. Maybe try smething like :

<%= f.fields_for :meal do |meal_f| %>

And in your partial :

<%= meal_f.text_field :name %>

Upvotes: 1

Related Questions