Wordica
Wordica

Reputation: 2595

Rails nested form_for

I don't understand why my file_field disappears when I set nested_attributes

MY MODELS:

class Item < ActiveRecord::Base
    attr_accessible :asset_attributes

     has_one :asset
     accepts_nested_attributes_for :asset
end


class Asset < ActiveRecord::Base

   attr_accessible :item_id, :photo
   belongs_to :item


end

MY HTML:

<%= form_for @item do |f| %>

    <%= f.fields_for :asset do |asset| %>
        <%= asset.file_field :photo %>
    <% end %>
<% end %>

Why, when I set accept_nested_attributes_for :asset, file_fields disappears?

This form_for is for existing @item. I first create @item because I need item_id - so form_for is for update action.

Upvotes: 0

Views: 68

Answers (1)

Ishank Gupta
Ishank Gupta

Reputation: 1593

You have to build a relational object of asset. In the controller action try :

def new
  @item = Item.new
  @item.build_asset
end

And for the edit action, if there is a association for item and asset...then only the nested attributes will appear

Upvotes: 2

Related Questions