简体   繁体   中英

Why put custom action does not work in rails 3.1?

A put custom action approve was added to our rails 3.1.3 app in sourcing controller for updating 3 parameters. Custom route was added in routes.rb and link_to was called with:method =>:put. However the parameters are not reset by the 'approve'.

Here is the link_to in sourcing index.html.erb:

<% @sourcings.each do |src| %>
...
<%= link_to 'Approve', approve_project_sourcing_path(@project, src), :method => :put if need_approve?(src) %>
<% end >

need_approve? is a method defined in sourcings controller to see if the sourcing needs to be approved.

Here is the rake routes output for approve:

approve_project_sourcing PUT    /projects/:project_id/sourcings/:id/approve(.:format)                                      {:action=>"approve", :controller=>"sourcings"}

Here is the approve in sourcing controller:

  def approve

    @project = Project.find(params[:project_id])
    @sourcing = @project.sourcings.find(params[:id]) 
    if vp_eng?
        @sourcing.update_attributes!(:approved_by_vp_eng => true, :approve_vp_eng_id => session[:user_id],
                                    :approve_date_vp_eng => Time.now, :as => :role_update)

      elsif ceo?
        @sourcing.update_attributes(:approved_by_ceo => true, :approve_ceo_id => session[:user_id],
                                    :approve_date_ceo => Time.now, :as => :role_update) 
    end
    redirect_to project_sourcing_path(@project, @sourcing)   

  end

Is anything we missed in the code above? Thanks so much.

The problem is with the way to call update_attributes. With hash parameters, the correct syntax is:

@sourcing.update_attributes({:approved_by_vp_eng => true, :approve_vp_eng_id => session[:user_id],:approve_date_vp_eng => Time.now}, :as => :role_update)

A brace bracket is needed for the hash parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM