0

我在获取一个指向控制器中正确操作的按钮时遇到问题。我的显示视图有以下按钮:

<%= 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'

我在创建这两个动作时添加了两个“匹配”行。有任何想法吗?

4

2 回答 2

1

问题是您的路由具有相同的路径ecns#submitecns#closeHTTP 动词。

rails 处理路由的方式是它会在你的中从上到下遍历,routes.rb并在找到与请求匹配的第一个路径时停止。所以,一个请求ecns/index总是会被发送到ecns#submit. 如果路径相同,路由器如何知道将其发送到哪个操作?

尝试为 指定不同的路径ecns#close,或为每个操作指定不同的 HTTP 动词,例如postfor#submitdeletefor #close

于 2013-11-03T19:21:15.293 回答
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?')" %>
于 2013-11-03T19:19:15.833 回答