ユーザーとアカウントを持つアプリケーションを作成しています。私の問題は、最初にユーザーモデルと認証機能を作成しましたが、ユーザーをアカウントに所属させる必要があることを認識したことです。
ルート
http://lvh.me:3000/signup
でユーザーをサインアップした場合新しいユーザーを作成し、アクティベーションメールを送信し、ユーザーをアクティベートします。
Account
を作成しないことを除いて、すばらしい動作をします
。しかし、今、
account
を追加する必要がありますに混ざった。新しいルート
http://lvh.me:3000/accounts/new
でサインアップした場合アカウントとユーザーが作成されますが、実際にユーザーをアクティブ化できるように、アクティベーションメールを送信する必要があります。
Account
を取得できないようです
@user.send_activation_email
をトリガーするコントローラーで
create
私の
UserController
内のアクション-以下のコードを参照してください。私はそれを下に持っている方法が正しい方法ではないことを知っていますが、レンガの壁にぶつかり、ここからどこに行くべきかわかりません。
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
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
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
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
...
回答 1 件
関連記事
- セキュアコントローラーは、Grailsアプリで1つのアクションを返しますが、別のアクションでは返しません。
- Railsのモデルとコントローラーの名前はRailsの予約語と衝突しますか?
- コントローラーのあるページから別のコントローラーのある別のページにIDを取得する方法
- 複数の子コンポーネントが1から別のアクションを呼び出す
- tableview did selectメソッドを使用してページを別のView Controllerに移動する方法
- 待機操作を実行しないWebAPIコントローラーアクションに非同期を設定する利点は何ですか?
- Ruby on Railsの別のドロップダウンに基づいてドロップダウンメニューを変更する
- ビューからMVC ASP NETの別のコントローラーに値を渡す方法
- 目標C:NSStringを別のView Controllerに渡すとnullが返されます
- 受信者は、デバイスを呼び出すときにscreen_onアクションを無視します
関連した質問
サインアップフローの一部として両方のモデルを作成する必要がある場合、サインアップフローをトリガーして両方のレコードを作成する単一のコントローラーで単一のアクションを実行します。
Users#signup
を使用するなど、さまざまな方法でこれを実装できます アクションは、トランザクション内でユーザーとアカウントを作成します。または、そのロジックをコントローラーからモデル層に移動して、User.signup
を提供できます。 アカウントを明示的にまたはafter_create
で作成する方法 折り返し電話。いずれにせよ、ここでの修正は、サインアップフローを複数のコントローラーに分割するのではなく、単純化して統合することです。ユーザーがステップ間で何らかのアクションを実行する必要があるマルチステップサインアップがある場合にのみ、これを行う必要があります。