1

I have three model:

 class Project < ActiveRecord::Base
  attr_accessible :name
  has_many :tickets, dependent: :delete_all
 end

 class Ticket < ActiveRecord::Base
  belongs_to :project
  attr_accessible :description, :title,:asset  
  has_many :assets
  accepts_nested_attributes_for :assets
end

class Asset < ActiveRecord::Base
  attr_accessible :title, :body
  belongs_to :ticket
  has_attached_file :asset
end

_form:

<%= form_for([@project,@ticket], html: { multipart: true }) do |f| %>
<p>
    <%= f.label :title %>
    <%= f.text_field :title %>
</p>
<p>
    <%= f.label :description %>
    <%= f.text_area :description %>
</p>

<% number = 0 %>
<%= f.fields_for :assets do |asset| %>
    <p>
        <%= asset.label "File ##{number +=1}" %>
        <%= asset.file_field :asset %>
    </p>
<% end %>
<%= f.submit %>
<% end %>

TicketsController:

def new
 @ticket = @project.tickets.build
 3.times { @ticket.assets.build }
end

The problem is when I try to create new ticket for a project it shows ActiveRecord::UnknownAttributeError at /projects/1/tickets/new unknown attribute: ticket_id @ticket= id: nil, title: nil, description: nil, project_id: 1, created_at: nil, updated_at: nil, user_id: nil

From error message I can see that tickets "id" is nil(it's not yet created), so assets don't have ticket_id, so how do I get around it?

3
  • This kind of nested form is a feature of nested_form gem. Are you including it on your Gemfile? Commented Mar 18, 2013 at 21:52
  • No, I am only using paperclip Commented Mar 18, 2013 at 21:59
  • nested_form gem is not essential here since he is building assets manually on the action file. Commented Mar 19, 2013 at 7:50

2 Answers 2

1
attr_accessible :ticket_id

You need to add this field as attr accessible on its model.

For nested attribuetes you also need to add project_assets as attr_accessibl/ attr_accessor

attr_accessible :project_assets
attr_accessor :project_assets
Sign up to request clarification or add additional context in comments.

Comments

0

You should use nested_form gem for assets and you have to create an attribute of ticket_id in assets database table to work association properly.

2 Comments

but it is possible with out using nested form gem
yes it is possible without using nested_form but i think better way to do this is using nested_gem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.