我在按钮指向控制器中的正确操作时遇到问题。我的节目视图有以下按钮:
<%= 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?')" %>
我的控制器有以下两个操作:
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
当我单击“提交审批”按钮时,提交操作将按预期运行。 当我单击“关闭ECN”按钮时,警报会按预期显示,但然后处理提交操作而不是关闭操作。单击“关闭ECN”按钮时,我的开发日志显示以下内容:
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
...
所以我可以看到它正在调用不正确的操作。我不确定会导致什么,我不太了解路由,但这里也是我的路由文件:
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'
我在创建两个动作时添加了两个“匹配”行。有什么想法吗?
答案 0 :(得分:1)
问题是ecns#submit
和ecns#close
的路由具有相同的路径和HTTP谓词。
rails处理路由的方式是它会在routes.rb
中从上到下进行,并在找到与请求匹配的第一条路径时停止。因此,对ecns/index
的请求将始终发送至ecns#submit
。如果路径相同,路由器如何知道将其发送到哪个动作?
尝试为ecns#close
指定不同的路径,或为每个操作指定不同的HTTP谓词,例如post
的{{1}}和#submit
的{{1}}。
答案 1 :(得分:0)
尝试使用路由中的路径进行关闭操作,并确保您根据路由使用正确的HTTP谓词(最有可能是POST与PUT):
<%= 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?')" %>