0

In my application I have a project ,for each project user can upload many assets. The Upload is done by carrier wave. I have two questions:

1)is there a better way of writing the create method?

2) How should I change my update method to update the attached files

The model,project.rb

class Project < ActiveRecord::Base

    belongs_to :user
    has_many :assets
end

the asset mode, asset.rb

class Asset < ActiveRecord::Base

   belongs_to :project
   belongs_to :user
   mount_uploader :attachment, AttachmentUploader #CarrierWave 


end

This is my Create method which works fine

  def create
      @project = Project.new(project_params)
      respond_to do |format|
    if @project.save
        if params[:assets] && params[:assets]['attachment'] 
            params[:assets]['attachment'].each do |a|
                @asset = @project.assets.create!(:attachment => a, :user_id=>@project.user.id) 
                format.html { redirect_to @project, notice: 'Project was successfully created.' }
                format.json { render :show, status: :created, location: @project }
        end
        else   
            format.html { render :new }
            format.json { render json: @project.errors, status: :unprocessable_entity }
     end
    else 
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
    end
end 
end


def project_params
  params.require(:project).permit(:user_id,  :summary, :start_date,assets_attributes: [:id, :project_id, :attachment,:user_id] )
end

here is the Update method

  def update
  respond_to do |format|
  if @project.update(project_params)

            format.html { render :edit }
            format.json { render json: @project.errors, status: :unprocessable_entity }

  else
    format.html { render :edit }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
end
user1876128
  • 91
  • 14

1 Answers1

0

You can use the accepts_nested_attributes_for method built into active record to easily handle creating, updating, and destroying nested models

http://railscasts.com/episodes/196-nested-model-form-revised?view=asciicast

EDIT FOR QUESTION CHANGES

You seem to have gone through many revisions to your original question, let me see if I can help clear some things up for you.

Let's assume that your asset class has a column called attachment and you have a carrierwave uploader class called AttachmentUploader. You can then mount that class to your asset class like you show in your question

 mount_uploader :attachment, AttachmentUploader #CarrierWave 

Your not showing your form markup in the question, but based on your controller's project_params method it seems that you're expecting a property called asset_attributes. You'll want to make sure that your form correctly builds that array, most easily accomplished by using the fields_for helper. If your confused about that part refer to this question Rails 4 Nested Attributes Unpermitted Parameters

Basically it should look like this

projects/_form.html.erb

<%= form_for @project, :html => {:multipart => true} do |f| %>
  <%= f.error_messages %>

  <%= f.fields_for :assets do |builder| %>
     <%= render 'assets_fields', :f => builder %>
  <% end %>
  <p><%= f.submit %></p>
<% end %>

projects/_assets_fields.html.erb

<p>
   <%= f.file_field :attachment %>
</p>

When the project form is submitted you don't need to loop over the assets manually determining if they are existing assets that are being updated, new assets that are being created, or assets flagged for deletion. That logic is all handled for you by adding this to your project class

accepts_nested_attributes_for :assets, :allow_destroy => true

Just calling

project.save 

or

project.update

will automatically create/update/destroy the nested assets wrapping the whole process in a transaction so it all succeeds or all fails. The only expection to that being that active record can't wrap carrier wave actions in a database transaction so you may end up with orphaned file uploads on your server.

Community
  • 1
  • 1
Ben Fischer
  • 6,382
  • 2
  • 17
  • 22
  • Thanks, I changed the models and still I have a problem. I updated the question – user1876128 Aug 07 '14 at 15:00
  • well based on your comment I tried to use nested_attributes together with Polymorphic association. still it does not save I ahev the details in this post Rails: (http://stackoverflow.com/questions/25186981/rails-polymorphic-assosiation-and-accepts-nested-attributes) – user1876128 Aug 08 '14 at 14:35