Note that there are some explanatory texts on larger screens.

plurals
  1. PONested attributes and interacting with form hash and setting the user_id to current_user.id
    text
    copied!<p>I have rewritten this question as the other one seemed too verbose. I have the following model structure and am interested in having a set of files uploaded (2 at a time via a builder). I am not sure if the polymorphic relat</p> <pre><code>class Item &lt; ActiveRecord::Base has_many :assets, :as =&gt; :assetable, :dependent =&gt; :destroy accepts_nested_attributes_for :assets, :allow_destroy =&gt; true, :reject_if =&gt; lambda { |a| a[:asset].blank? } attr_accessible :assets_attributes, :asset ... end class Asset &lt; ActiveRecord::Base belongs_to :assetable, :polymorphic =&gt; true attr_accessible :name, :user_id, :description ... ... end </code></pre> <p>I have copied out the entirety of the form: </p> <pre><code>&lt;form accept-charset="UTF-8" action="/arc/items/70" class="edit_item" enctype="multipart/form-data" id="edit_item_70" method="post"&gt;&lt;div style="margin:0;padding:0;display:inline"&gt;&lt;input name="utf8" type="hidden" value="&amp;#x2713;" /&gt;&lt;input name="_method" type="hidden" value="put" /&gt;&lt;input name="authenticity_token" type="hidden" value="NnaCxwmfy2zT8MnN7oNQnOx0xYXkgKUs+NbIh8+8cL4=" /&gt;&lt;/div&gt; &lt;div class='files'&gt; &lt;input id="item_assets_attributes_70_asset" name="item[assets_attributes][70][asset]" type="file" /&gt; &lt;input id="item_assets_attributes_70_description" name="item[assets_attributes][70][description]" size="30" type="text" /&gt; &lt;input id="item_assets_attributes_71_asset" name="item[assets_attributes][71][asset]" type="file" /&gt; &lt;input id="item_assets_attributes_71_description" name="item[assets_attributes][71][description]" size="30" type="text" /&gt; &lt;/div&gt; &lt;input name="commit" type="submit" value="Update Menu item" /&gt; &lt;/form&gt; </code></pre> <p>and here is the controller for interacting with it: </p> <pre><code>class ItemsController &lt; ApplicationController ... def update item=params[:item] item[:assets_attributes].values do |v| v[:asset][:user_id]=3 # just hard-code it to see if workig end if @item.update_attributes(item) flash[:notice]="Menu Item has been updated #{undo_link}" else flash[:notice]="Menu Item has not been updated" end end </code></pre> <p>In my asset class, the user_id NEVER gets set. How would I make this work? I obviously want to get the current_user.id.</p> <p>thx</p> <p><strong>edit #1</strong> </p> <p>Using Rails 3.1</p> <p><strong>edit 2</strong></p> <p>One thing that I've noticed is that if I set an instance variable in the loop and then debug it into the view, it wasn't set meaning that this loop isn't even running. </p> <pre><code>item[:assets_attributes].values do |v| v[:user_id]=3 @a="here is a string" end </code></pre> <p>I have also updated the Item model with the appropriate attr_accessible for the item class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload