Question

I'm having issues getting a button to point to the correct action in my controller. My show view has the following buttons:

<%= button_to "Submit for Approval", {action: "submit", :id => @ecn.id}  %>
<%= button_to "Close ECN", {action: "close", :id => @ecn.id}, :onclick => "return confirm('Once an ECN is closed it can no longer be edited, are you sure you want to close this ECN?')" %>

My controller has the following two actions:

  def submit

    @ecn = Ecn.find(params[:id])
    @email_list = EmailList.all

    respond_to do |format|
      EcnNotifier.submit_engineering(@ecn).deliver if @ecn.distribute_engineering?
      EcnNotifier.submit_purchasing(@ecn).deliver if @ecn.distribute_purchasing?
      EcnNotifier.submit_manufacturing(@ecn).deliver if @ecn.distribute_manufacturing?
      EcnNotifier.submit_qantel(@ecn).deliver if @ecn.distribute_qantel?
      EcnNotifier.submit_planning(@ecn).deliver if @ecn.distribute_planning?
      format.html { redirect_to ecns_url, alert: "Ecn has been submitted for approval." }
      format.json { render json: @ecns }
    end
  end

  def close
    @ecn = Ecn.find(params[:id])
    @ecn = @ecn.close_status

    respond_to do |format|
      EcnNotifier.close_engineering(@ecn).deliver if @ecn.distribute_engineering?
      format.html { redirect_to ecns_url, alert: "Ecn has been closed.  A confirmation email has been sent to the appropriate personnel." }
      format.json { render json: @ecns }
    end
  end

When I click the "Submit for Approval" button, the submit action runs as expected. When I click the "Close ECN" button, the alert comes up as expected, but then the submit action is processed rather than the close action. My development log shows the following when I click the "Close ECN" button:

    Started POST "/ecns/index?id=34" for 127.0.0.1 at 2013-11-03 11:53:02 -0700
    Processing by EcnsController#submit as HTML
    ...

So I can see that it is calling the incorrect action. I'm not sure what would cause this, I don't know too much about routing, but here is my routes file as well:

Engdb::Application.routes.draw do

  resources :email_lists

get 'home' => 'home#index'
get 'logout' => 'sessions#destroy'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'

  get "login/index"

  get "sessions/new"

  get "sessions/create"

  get "sessions/destroy"

  resources :users

  get "home/index"

  resources :ecns

  resources :revisions

  resources :drawings

  resources :home

 match 'ecns/index' => 'ecns#submit'
 match 'ecns/index' => 'ecns#close'

I added the two "match" lines when I created the two actions. Any ideas?

Was it helpful?

Solution

The problem is that your routes for ecns#submit and ecns#close have identical paths and HTTP verbs.

The way rails handles routes is that it will go from top to bottom in your routes.rb and stop when it finds the first path that matches the request. So, a request to ecns/index will always get sent to ecns#submit. If the path is identical, how would the router known which action to send it to?

Try specifying a different path for ecns#close, or specifying different HTTP verbs for each action, such as post for #submit and delete for #close.

OTHER TIPS

Try using the path in routes for the close action, and ensure you're using the right HTTP verb based on routes (POST vs. PUT most likely):

<%= button_to "Close ECN", {:url => <close_path>, :method => <:put/:post>, :id => @ecn.id}, :onclick => "return confirm('Once an ECN is closed it can no longer be edited, are you sure you want to close this ECN?')" %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top