按钮指向控制器中的错误操作



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

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

我在创建两个操作时添加了两个"匹配"行。 有什么想法吗?

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

rails 处理路由的方式是,它将在您的routes.rb中从上到下移动,并在找到与请求匹配的第一个路径时停止。因此,对ecns/index的请求将始终发送到 ecns#submit .如果路径相同,路由器如何知道要将其发送到哪个操作?

尝试为 ecns#close 指定不同的路径,或为每个操作指定不同的 HTTP 谓词,例如 post 表示 #submitdelete 表示 #close

尝试在路由中使用路径进行关闭操作,并确保根据路由使用正确的 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?')" %>

相关内容

  • 没有找到相关文章

最新更新