Wednesday, 15 June 2011

ruby - saving documents in rails controller -



ruby - saving documents in rails controller -

i have model, "grievance". grievance can have many documents attached it. documents can associated kinds of things polymorphic.

here grievance model

class grievance < activerecord::base belongs_to :account belongs_to :employee has_many :documents, :class_name => "employeedocument", :as => 'documentable' accepts_nested_attributes_for :documents, :allow_destroy => true end

the show page of grievance allows user upload multiple documents associated grievance. works well.

i refactoring code of developer, , looking @ update action in controller. code looks this...

def update @grievance = @employee.grievances.find(params[:id]) update! { flash[:notice] = 'updated successfully' redirect_to edit_employee_grievance_path(:employee_id => @employee.id, :id => @grievance, :tab_to_return_to => params[:tab_to_return_to]) , homecoming } render :form end

whilst works fine, wanted refactor it, create more readable learn. changed this.

def update @grievance = @employee.grievances.find(params[:id]) if @grievance.save flash[:notice] = "#{@grievance.grievance_type} record updated" redirect_to employee_grievance_path(@employee, @grievance) , homecoming else flash[:alert] = "there problem editing record" render :edit end

now appreciate code more advanced more, , more concise, trying understand why code save documents, , mine not. can see in log form passing details of document controller, must update code?

in version, there nil beingness done @greivance between beingness loaded, , saved.

you missing this:

@grievance.update_attributes(params[:grievance])

inside params attributes form set values of @grievance, it's nested attributes save attached documents.

the other developer's version using inherited resources, automatically. overriding functionality different inheritedresources default.

ruby-on-rails ruby

No comments:

Post a Comment