[英]Calling a Rails 5 action from one controller to another
I am creating an application that has users and accounts. 我正在创建一个具有用户和帐户的应用程序。 My issue is that I created the users model and authentication function first but then realized I need to have users belong to accounts.
我的问题是,我首先创建了用户模型和身份验证功能,但后来意识到我需要让用户属于帐户。
If I signup a user at route http://lvh.me:3000/signup
it will create a new user, send an activation email and activate a user. 如果我在路由
http://lvh.me:3000/signup
注册了用户,它将创建一个新用户,发送激活电子邮件并激活该用户。 Works great except it doesn't create the Account
. 效果很好,除非它不会创建
Account
。 But now, I need to add an account
in the mix. 但是现在,我需要在
account
中添加一个account
。 If I sign up at my new route http://lvh.me:3000/accounts/new
it will create the account and the user, but I need to send the activation email so I can actually activate the user. 如果我在新路径
http://lvh.me:3000/accounts/new
,它将创建帐户和用户,但是我需要发送激活电子邮件,以便实际上可以激活用户。 I can't seem to get my Account
controller to trigger the @user.send_activation_email
in the create
action inside my UserController
-- see code below. 我似乎无法让我的
Account
控制器在UserController
内部的create
动作中触发@user.send_activation_email
-请参见下面的代码。 I know the way I have it below is not the right way, but I've hit a brick wall and not sure where to go from here. 我知道我在下面找到它的方法不是正确的方法,但是我碰到了墙,不知道从这里去哪里。
user.rb user.rb
class User < ApplicationRecord
has_many :memberships
has_many :accounts, through: :memberships
accepts_nested_attributes_for :accounts
...
# Sends activation email.
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
...
account.rb account.rb
class Account < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
accepts_nested_attributes_for :owner
has_many :memberships
has_many :users, through: :memberships
end
accounts_controller.rb account_controller.rb
class AccountsController < ApplicationController
def new
@account = Account.new
@account.build_owner
end
def create
@account = Account.new(account_params)
if @account.save
@user.send_activation_email
flash[:info] = 'Please check your email to activate your account.' # Use this for registered users
# flash[:info] = 'Please have user check their email to activate their account.' # Use this for admin created users
redirect_to root_url
else
flash.now[:alert] = 'Sorry, your account could not be created.'
render :new
end
end
private
def account_params
params.require(:account).permit(:organization, owner_attributes: [:name, :email, :password, :password_confirmation])
end
end
users_controller.rb users_controller.rb
class UsersController < ApplicationController
...
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = 'Please check your email to activate your account.' # Use this for registered users
# flash[:info] = 'Please have user check their email to activate their account.' # Use this for admin created users
redirect_to root_url
else
render 'new'
end
end
...
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, accounts_attributes: [:organization])
end
...
If you need both models created as part of the signup flow, then have a single action in a single controller that triggers your signup flow and creates both records. 如果您需要将两个模型都创建为注册流程的一部分,则可以在单个控制器中执行一次操作,以触发您的注册流程并创建两个记录。
You can implement this in a number of ways, such as having the Users#signup
action create the user and the account inside a transaction, or you can move that logic out of your controller and into your model layer and provide a User.signup
method that creates the account explicitly or in an after_create
callback. 您可以通过多种方式实现此目的,例如让
Users#signup
操作在事务内创建用户和帐户,或者可以将该逻辑移出控制器并移至模型层并提供User.signup
方法。明确创建帐户或在after_create
回调中创建帐户。
Either way, the fix here is to simplify and unify your signup flow, not to split it up across several controllers. 无论哪种方式,这里的解决方法都是简化并统一您的注册流程,而不是将其拆分到多个控制器中。 You only ever need to do that if you have some kind of multi-step signup that requires the user to perform some action between steps.
仅当您具有某种需要用户在步骤之间执行某些操作的多步骤注册时,才需要这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.