I have a model User and StockShipment.
I'm trying to create various StockShipments for my user from the StockShipment new action.
For this, I use in my User model:
accepts_nested_attributes_for :stock_shipments, reject_if: :all_blank, allow_destroy: true
In my stockshipments/new.html.erb
<%= form_with model: [@user, @stock_shipment] do |form| %>
<%= form.fields_for :stock_shipments, @user.stock_shipments.build do |shipment_form| %>
<%= render 'shared/shipment_fields', form: shipment_form, locals: { user: current_user, user_stock: @user_stock } %>
<% end %>
In my Stock Shipments controller:
def new
@user = current_user
@stock_shipment = StockShipment.new
@user.stock_shipments.build
end
def create
@user = current_user
@user_stock = UserStock.find(params[:stock_shipment][:user_stock_id])
@stock_shipment = @user.stock_shipments.build(stock_shipments_params)
if @stock_shipment.save
ShipmentNotificationMailer.new_shipment_notification(@stock_shipment).deliver_later
redirect_to user_stock_shipments_path(current_user), status: :see_other
else
puts @stock_shipment.errors.full_messages.inspect
end
end
Lastly, I have my strong parameters to accept nested attributes:
def stock_shipments_params
params.require(:stock_shipment).permit(
:country, :phone, :email,
:name, :quantity, :address, :city, :province, :postalcode, :surname, :user_id, :campaign_id, :gift_selection_id, :delivery_date, :stock_variation, :description, :user_stock_id,
stock_shipments_attributes: [:id, :country, :phone, :email, :name, :quantity, :address, :city, :province, :postalcode, :surname, :user_id, :campaign_id, :gift_selection_id, :delivery_date, :stock_variation, :description, :user_stock_id, :_destroy]
)
end
I'm getting the following error all the time:
Unpermitted parameter: :stock_shipments.
My params being passed:
Started POST "/users/388/stock_shipments?locale=es" for ::1 at 2023-12-17 13:58:03 +0100
13:58:03 web.1 | Processing by StockShipmentsController#create as TURBO_STREAM
13:58:03 web.1 | Parameters: {"authenticity_token"=>"[FILTERED]", "stock_shipment"=>{"stock_shipments"=>{"quantity"=>"1", "name"=>"asdf", "surname"=>"asdf", "address"=>"sadf", "postalcode"=>"213", "city"=>"asdf", "province"=>"asdf", "country"=>"bg", "phone"=>"234", "email"=>"[email protected]", "delivery_date"=>"2023-12-20", "user_id"=>"388", "_destroy"=>"false"}, "user_stock_id"=>"84", "user_id"=>"388"}, "user"=>{"stock_shipments_attributes"=>{"1702817879"=>{"quantity"=>"30", "name"=>"María", "surname"=>"Jisd", "address"=>"Cadsf", "postalcode"=>"238", "city"=>"Madrid", "province"=>"adsf", "country"=>"es", "phone"=>"645455555", "email"=>"[email protected]", "delivery_date"=>"2023-12-20", "user_id"=>"388", "_destroy"=>"false"}}}, "commit"=>"Confirmar envíos", "locale"=>"es", "user_id"=>"388"}
You're not showing the partial, which is where some of the issue is going to be, but you're sending
user_stock: @user_stockinto it and yet@user_stockisn't defined anywhere.Mostly though, you've just got a lot of unnecessary junk in your
form_withandfields_forcalls. You should be following the nested form guide for fun and profit.