Search code examples
ruby-on-railsruby-on-rails-5strong-parametersaccepts-nested-attributes

Why am I getting this unpermitted params error?


I have a PortStock model that looks like this:

class PortStock < ApplicationRecord
  has_many :closed_positions, dependent: :destroy
  accepts_nested_attributes_for :closed_positions, allow_destroy: true
end

My ClosedPosition model look like this:

class ClosedPosition < ApplicationRecord
  belongs_to :closer, class_name: "PortStock", foreign_key: "closer_id"
  belongs_to :closed, class_name: "PortStock", foreign_key: "port_stock_id" 
end

In my PortStockController, my params looks like this:

def port_stock_params
  params.require(:port_stock).permit(:portfolio_id, :stock_id, :closed_position, closed_positions_attributes: [:num_units, :closer_id, :port_stock_id, :closed_price, :_destroy])
end

This is the form:

<%= simple_form_for @port_stock, url: port_stocks_sell_order_path, method: :post, html: { class: "form-inline" } do |f| %>

  <% @buy_port_stocks.each do |port_stock| %>
    <%= f.simple_fields_for @cp, html: { class: "form-inline" } do |c| %>
      <div class="form-group">
        <%= c.input_field :port_stock_id, as: :hidden, value: port_stock.id %>
        <%= c.input_field :num_units, id: "sell-ps-#{port_stock.id}", placeholder: "Number of Units", class: "form-control mx-sm-3" %>
         <%= c.input_field :closed_price, id: "sale-price-for-ps-#{port_stock.id}", placeholder: "Sale Price", class: "form-control mx-sm-3" %>
       </div>
   <% end %>
  <% end %>
<% end %>

This is my PortStock#Create action:

@port_stock = current_user.port_stocks.friendly.find(params[:id])
@stock = @port_stock.stock
@cp = @port_stock.closed_positions.build

This is the error when I try to do a create:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"M/TCcc79QcfOvoIc3z/cIFn83sjA==", "port_stock"=>{"closed_position"=>{"port_stock_id"=>"10", "num_units"=>"25", "closed_price"=>"9"}}, "commit"=>"Sell Stock", "id"=>"CAC"}
  User Load (1.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ /.rvm/gems/ruby-2.5.1/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/active_record.rb:17
  PortStock Load (1.6ms)  SELECT  "port_stocks".* FROM "port_stocks" INNER JOIN "portfolios" ON "port_stocks"."portfolio_id" = "portfolios"."id" WHERE "portfolios"."user_id" = $1 AND "port_stocks"."ticker" = $2 LIMIT $3  [["user_id", 2], ["ticker", "CAC"], ["LIMIT", 1]]
  ↳ /.rvm/gems/ruby-2.5.1/gems/friendly_id-5.2.4/lib/friendly_id/finder_methods.rb:60
Unpermitted parameter: :closed_position

What could be causing this?


Solution

  • First,

    as you accepts the closed_positions_attributes as nested attributes, thus no need to add closed_position in strong parameters. If you still want to access closed_position attribute in PortStock, then in the model use attr_accessor

    Second, change the nested form like the following to get the required parameters.

    f.simple_fields_for :closed_positions, @cp, html: { class: "form-inline" } do |c|
    

    Hope it resolve the issue!