我在获取按钮以指向控制器中的正确操作时遇到问题。我的表演视图具有以下按钮:
<%= 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'
创建两个动作时,添加了两个“匹配”行。有任何想法吗?