gpt4 book ai didi

ruby-on-rails - Rails 3.2 `link_to`(在电子邮件中) `method: :put` 仍在生成 GET 请求

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:18 26 4
gpt4 key购买 nike

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

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

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 命名空间的所有路由:

# 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

这是 Controller Action :

# 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 routes:

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

最佳答案

这是因为这是电子邮件上下文中的链接。这是documented behavior :

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,如上面的引文所述。

关于ruby-on-rails - Rails 3.2 `link_to`(在电子邮件中) `method: :put` 仍在生成 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25573000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com