【发布时间】:2014-10-23 17:51:48
【问题描述】:
在我的应用程序中,我有自动电子邮件提醒应用程序完成面试过程的下一步。该电子邮件有一个退出链接,单击该链接时,应该会触发一个触发状态机事件以将其状态更改为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
这是控制器动作:
# 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
【问题讨论】:
-
link_to怎么可能是PUT?link_to不能有字段/表单,那么将其设置为PUT或POST有什么意义? -
我没有关注你,因为你问了两个看起来至少是半修辞的问题。你对我应该改变什么有什么建议吗?
-
为什么要将 link_to 作为 PUT?
-
我认为我必须这样做,因为路线是
PUT路线。实际上没有对应于我链接到的控制器操作的视图。新手来了!那么删除method:声明会有帮助吗? -
是的,只需将其删除.. 如果您遇到有关路由的错误,请向我显示与控制器相关的所有路由
标签: ruby-on-rails ruby email http hyperlink