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?