如何将after_sign_up_path_for重定向到另一个控制器的创建动作? [英] How do I redirect after_sign_up_path_for to a create action of another controller?

查看:117
本文介绍了如何将after_sign_up_path_for重定向到另一个控制器的创建动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有 OrdersController#Create ,我希望用户在注册后立即重定向到该用户(以便它可以执行一些注册后的工作)。 / p>

在实现该工作流程的注册部分之前,该资源的 link_to 如下所示:

  <%= link_to'Submit to Scheduling',orderspath(cart_id:@cart),方法::post,数据:{确认: 你确定吗? },类:主按钮btn%> 

所以基本上,我想实现上述功能(包括传递 @cart 对象作为参数),但会自动从 Devise :: RegistrationsController#Create 内部。



我正在使用Devise,因此我创建了 /users/registrations_controller.rb ,然后在该控制器中执行以下操作:

  def after_sign_up_path_for(资源)
orders_path(cart_id:@cart)
超级(资源)
结束

当我执行上述操作时,它成功创建了用户并将我重定向到 Orders#Index 这不是我想要的,请参见以下日志:

 用户创建(1.3ms)插入INTO用户(电子邮件, encrypted_pa​​ssword, first_name, last_name, created_at, updated_at, company_name, company_title,电话)值($ 1,$ 2,$ 3,$ 4 ,$ 5,$ 6,$ 7,$ 8,$ 9)返回 id [[电子邮件, abc3@test.com] ,[ encrypted_pa​​ssword, $ 2a $ 11 $ ZC2X2vCXd5JwVO],[ first_name, Test],[ last_name, User 3],[ created_at, 2020-01-22 06:16 :11.358863],[ updated_at, 2020-01-22 06:16:11.358863],[ company_name, Acme Inc],[ company_title, CFO],[电话,  9876543210]] 
↳app / controllers / users / registrations_controller.rb:14:in'create'
(0.6ms)COMMIT
↳app / controllers / users / registrations_controller.rb: 14:in`create'
重定向到http:// localhost:3000 / orders
已完成302在162ms内找到(ActiveRecord:5.3ms |分配:6383)


在2020-01-22 01:16:11 -0500开始为:: 1的:: 1获取``/ orders''(订单)以HTML形式由OrdersController#index处理

我什至尝试了 orders_path(cart_id:@cart,method::post) 却不起作用。



我如何实现自己的目标?



编辑1



所以我发现 url_for ,它的ALMOST我在那里,但工作不正常。



这就是我所拥有的:

  url_for(控制器:'/ orders',操作:'create',方法::post,cart_id:@ cart.id,only_path:true)

会发生以下情况:

 ↳应用程序/控制器/用户/registrations_controller.rb:14:in'create'
用户创建(5.5ms)插入用户(电子邮件, encrypted_pa​​ssword, first_name, last_name, created_at, updated_at , company_name, company_title, phone)值($ 1,$ 2,$ 3,$ 4,$ 5, $ 6,$ 7,$ 8,$ 9)返回 id [[ email, abc2@test.com],[ encrypted_pa​​ssword, $ 2a $ ezj6],[ first_name, Test], [ last_name, User 2],[ created_at, 2020-01-22 07:26:09.589560],[ updated_at, 2020-01-22 07:26:09.589560],[ company_name, Acme Inc],[ company_title, CEO],[电话, 9876543210]]
↳app / controllers / users / registrations_controller.rb:14:in`create '
(1.1ms)COMMIT
↳app / controllers / users / registrations_controller.rb:14:in'create'
/.rvm/gems/ruby-2.7.0@myapp/gems /devise-4.7.1/app/controllers/devise_controller.rb:187:警告:不建议将最后一个参数用作关键字参数;也许应该将**添加到调用
.rvm / gems / ruby​​-2.7.0 @ myapp / gems / i18n-1.8.1 / lib / i18n.rb:195:警告:调用的方法't'在此处定义
重定向到http:// localhost:3000 / orders?cart_id = 10& method = post
已完成302在307ms内找到(ActiveRecord:13.2ms |分配:8600)


在2020年1月22日02:26:09 -0500上为:: 1开始GET / orders?cart_id = 10& method = post为:: 1由OrdersController#index作为HTML $处理b $ b

所以它仍然不起作用。仍然将我发送至 OrdersController#Index

解决方案

不需要 super(resource)



据我所知,您想重定向到orders_path(cart_id:@购物车),方法为 post 。太奇怪了 after_sign_up_path_for 应该返回一个some_path,该路径可以通过 get 方法访问。



您可以尝试其他方式,从另一个控制器执行另一个操作。我认为将对您有所帮助。


So I have OrdersController#Create, that I would like the user to be redirected to right after they register (so it can do some post-registration stuff).

Before I implemented the registration part of this workflow, this is what the link_to for that resource looked like:

<%= link_to 'Submit to Scheduling', orders_path(cart_id: @cart), method: :post, data: { confirm: "Are you sure?" }, class: "primary button btn" %>

So basically, I would like to achieve the above functionality (including passing the @cart object as a param), but automatically from within the Devise::RegistrationsController#Create.

I am using Devise, and so I have created a /users/registrations_controller.rb and in that controller I am doing this:

  def after_sign_up_path_for(resource)
    orders_path(cart_id: @cart)
    super(resource)
  end

When I did the above, it successfully created the user and redirected me to Orders#Index which is not what I want, see the logs below:

 User Create (1.3ms)  INSERT INTO "users" ("email", "encrypted_password", "first_name", "last_name", "created_at", "updated_at", "company_name", "company_title", "phone") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["email", "abc3@test.com"], ["encrypted_password", "$2a$11$ZC2X2vCXd5JwVO"], ["first_name", "Test"], ["last_name", "User 3"], ["created_at", "2020-01-22 06:16:11.358863"], ["updated_at", "2020-01-22 06:16:11.358863"], ["company_name", "Acme Inc"], ["company_title", "CFO"], ["phone", "9876543210"]]
  ↳ app/controllers/users/registrations_controller.rb:14:in `create'
   (0.6ms)  COMMIT
  ↳ app/controllers/users/registrations_controller.rb:14:in `create'
Redirected to http://localhost:3000/orders
Completed 302 Found in 162ms (ActiveRecord: 5.3ms | Allocations: 6383)


Started GET "/orders" for ::1 at 2020-01-22 01:16:11 -0500
Processing by OrdersController#index as HTML

I even tried orders_path(cart_id: @cart, method: :post) and that didn't work.

How do I achieve what I am trying to do?

Edit 1

So I discovered url_for and it ALMOST gets me there, but doesn't quite work.

This is what I have:

url_for(controller: '/orders', action: 'create', method: :post, cart_id: @cart.id, only_path: true)

This is what happens:

↳ app/controllers/users/registrations_controller.rb:14:in `create'
  User Create (5.5ms)  INSERT INTO "users" ("email", "encrypted_password", "first_name", "last_name", "created_at", "updated_at", "company_name", "company_title", "phone") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["email", "abc2@test.com"], ["encrypted_password", "$2a$ezj6"], ["first_name", "Test"], ["last_name", "User 2"], ["created_at", "2020-01-22 07:26:09.589560"], ["updated_at", "2020-01-22 07:26:09.589560"], ["company_name", "Acme Inc"], ["company_title", "CEO"], ["phone", "9876543210"]]
  ↳ app/controllers/users/registrations_controller.rb:14:in `create'
   (1.1ms)  COMMIT
  ↳ app/controllers/users/registrations_controller.rb:14:in `create'
/.rvm/gems/ruby-2.7.0@myapp/gems/devise-4.7.1/app/controllers/devise_controller.rb:187: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
.rvm/gems/ruby-2.7.0@myapp/gems/i18n-1.8.1/lib/i18n.rb:195: warning: The called method `t' is defined here
Redirected to http://localhost:3000/orders?cart_id=10&method=post
Completed 302 Found in 307ms (ActiveRecord: 13.2ms | Allocations: 8600)


Started GET "/orders?cart_id=10&method=post" for ::1 at 2020-01-22 02:26:09 -0500
Processing by OrdersController#index as HTML

So it still doesn't work. It is still sending me to OrdersController#Index.

解决方案

You don't need super(resource).

In my understanding, you want to redirect to orders_path(cart_id: @cart) with post method. It's so weird. after_sign_up_path_for should return a some_path, which accessed by get method.

You can try another way, perform another action from another controller. I think this will helpful to you.

这篇关于如何将after_sign_up_path_for重定向到另一个控制器的创建动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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