Rails 3.2 `link_to`(在电子邮件中)使用 `method::put` 仍然产生 GET 请求 [英] Rails 3.2 `link_to` (in email) with `method: :put` still producing GET request

查看:14
本文介绍了Rails 3.2 `link_to`(在电子邮件中)使用 `method::put` 仍然产生 GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有自动电子邮件提醒应用程序完成面试过程的下一步.电子邮件有一个选择退出链接,当点击该链接时,应触发控制器动作,触发状态机事件以将其状态更改为 opted_out.链接不起作用,从本地主机控制台看来,这似乎是因为链接仍在生成 GET 请求,而该请求没有路由(错误是 ActionController::RoutingError (Not Found):).

In my app I have automated e-mails reminding applications to complete the next step in the interview process. The e-mail has an opt out link which, when clicked, should hit a controller action that fires a state machine event to change their state to opted_out. The link is not working, and from the localhost console it seems to be because the link is still producing a GET request, for which there is no route (the error is ActionController::RoutingError (Not Found):).

这是显示不需要的 GET 请求的控制台:

Here is the console displaying the undesired GET request:

Started GET "/worker/application/opt_out.1" for 10.0.2.2 at 2014-08-29 17:08:06 +0000
Processing by LandingController#show as 
  Parameters: {"category"=>"worker/application", "location"=>"opt_out"}

这是链接:

link_to 'stop getting these reminders', opt_out_worker_application_url(@worker), method: :put, style: 'background-color: #fff; color: #56A0D3; display: inline-block; margin-bottom: 5px; margin: 0px; padding: 0px; text-decoration: none'

以下是 worker 命名空间的所有路由:

Here are all the routes for the worker namespace:

# routes.rb

  namespace :worker do
    resource :application, only: [:create, :new, :show] do
      member do 
        put 'opt_out' => 'application#opt_out'
      end
    end
    resources :jobs do
      member do
        post 'compete' => 'jobs#compete'
        delete 'compete' => 'jobs#withdraw'
      end
      resources :comments, only: [:create]
      resources :timesheets, only: [:create, :new, :show]
    end
    resources :banks, only: [:create, :new, :destroy]
    scope 'money' do
      root to: 'money#index', as: 'money'
      post 'withdraw' => 'money#withdraw', as: 'money_withdraw'
    end
    get 'profile' => 'profiles#show'
    root to: 'jobs#index'
  end

这是控制器操作:

# applications_controller.rb

      def opt_out
        @worker = Worker.find(params[:id])
        if @worker.interview_requested? or @worker.onboarding_requested?
          if @worker.fire_state_event(:opt_out)
            AdminsMailer.sweeper_opted_out(@worker.id)
            redirect_to root_url, notice: "You have successfully opted out of the application process. We're sorry to see you go, and hope you'll consider returning in the future!"
          else
            redirect_to root_url, alert: "There was a problem. Please contact Support if you need further assistance."
          end
        else
          redirect_to root_url, alert: "There was a problem. Please contact Support if you need further assistance."
        end
      end

这是rake路线:

opt_out_worker_application PUT    /worker/application/opt_out(.:format)           worker/application#opt_out
                worker_application POST   /worker/application(.:format)                   worker/applications#create
            new_worker_application GET    /worker/application/new(.:format)               worker/applications#new
                                   GET    /worker/application(.:format)                   worker/applications#show

推荐答案

这是因为这是电子邮件上下文中的链接.这是记录的行为:

It's because this is a link in context of an email. It's documented behavior:

method:HTTP 动词的符号 - 此修饰符将动态创建一个HTML 表单并立即提交表单以使用指定的 HTTP 动词.用于让链接执行 POST 操作在危险的操作中,例如删除记录(搜索机器人可以在抓取您的网站时关注).支持的动词是 :post, :delete,:patch 和 :put.请注意,如果用户禁用了 JavaScript,则请求将回退到使用 GET.如果使用 href: '#' 并且用户禁用 JavaScript 单击链接将无效.如果你依赖于 POST 行为,您应该在您的通过使用请求对象的方法发布控制器的动作?,删除?,修补?或放置?.

method: symbol of HTTP verb - This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete, :patch, and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET. If href: '#' is used and the user has JavaScript disabled clicking the link will have no effect. If you are relying on the POST behavior, you should check for it in your controller's action by using the request object's methods for post?, delete?, patch?, or put?.

出于安全原因,禁止在电子邮件和表单中使用可执行的 JavaScript.因此,不可能使用 JS 呈现表单并执行 PUT 请求,因此这会退回到 GET,如上面引文所述.

There are security reasons to disallow executable JavaScript in emails, as well as forms. Therefore, it's impossible to render a form with JS and execute a PUT request, so this falls back to GET as stated in citation above.

这篇关于Rails 3.2 `link_to`(在电子邮件中)使用 `method::put` 仍然产生 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆