0

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?

marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • in your strong parameters, where did `closed_position` came from? – Emu Jun 27 '18 at 07:05
  • @Emu Do you mean in my declaration in the controller? If so, I just added that parameter just to make sure it wasn't the issue. If you mean in the parameter that is passed by the controller (i.e. in the logs), I am not sure. That's confusing me too, hence why I included the form code. – marcamillion Jun 27 '18 at 07:06
  • 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` – Emu Jun 27 '18 at 07:09
  • @Emu ok fair enough. I took it out but I am still having the same issue. – marcamillion Jun 27 '18 at 07:10
  • Do you have `closed_position` in your `PortStock` table? – Akash Pinnaka Jun 27 '18 at 07:11
  • @marcamillion I'm not sure why your parameters not have `"port_stock"=>{"closed_positions_attributes"..` instead of that you are getting `closed_postion` for nested attributes parameters. – Anand Jun 27 '18 at 07:12
  • @AkashPinnaka No I don't. `PortStock has_many :closed_positions`. – marcamillion Jun 27 '18 at 07:12
  • @Gabbar Yep I realized that. That's what I am trying to figure out how to fix. – marcamillion Jun 27 '18 at 07:13
  • Is `:closed_position` column in any other table in your database? – Akash Pinnaka Jun 27 '18 at 07:18
  • @marcamillion Have you defined `@buy_port_stocks` in new action? – Anand Jun 27 '18 at 07:19
  • Also, you are not passing `:closed_position` from the form. You have any hidden field? – Akash Pinnaka Jun 27 '18 at 07:19
  • @AkashPinnaka there is no column called `:closed_position` in any other table in the db. I am just trying to be able to pass `closed_positions` without doing `:closed_positions`, because it generates multiple objects per the form. – marcamillion Jun 27 '18 at 07:20
  • @Gabbar I have defined it in my new action: `@buy_port_stocks = current_user.port_stocks.buy.joins(:stock).where(stocks: { ticker: @stock.ticker})` – marcamillion Jun 27 '18 at 07:21
  • @marcamillion I'm not sure, could you give a try below given answer, i think you should build closed_postions for each `port_stock` model in this way you could get `closed_postions_attributes` at controller? – Anand Jun 27 '18 at 07:25

1 Answers1

2

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!

Emu
  • 5,763
  • 3
  • 31
  • 51