前提
・ログイン状態の新規ユーザーに対する動作を想定。
・deviseのメールconfirmで、userテーブルにusernameとemailだけは登録済。
・新規登録フォームは、profileテーブルとauthorテーブルの二つに関係。
やりたいこと
・新規ユーザーが新規登録フォームを済まさずに、サイト内のURLを直接打ち込んでアクセスした場合、自動的に新規登録フォーム1枚目(profiles/new)へ送りたい。
・ケース2として、新規ユーザーがprofileだけを登録しただけの状態で、サイト内の他ページにアクセスした場合、新規登録フォーム2枚目(authors/new)へ送りたい。
試してみたこと
この方法が最適ではないかと思いますが、下記のようなコードを書いてました。
ruby
1class ApplicationController < ActionController::Base 2 3before_action :registration 4 5 def registration 6 if not user_signed_in? 7 elsif Profile.find_by(id: current_user.id).blank? 8 redirect_to("/profiles/new") 9 elsif Author.find_by(id: current_user.id).blank? then 10 redirect_to("/authors/new") 11 else 12 end 13 end
この時、例えばauthorsが登録されていない状態で、"authors/new"にアクセスすると、"/profiles/new"へページ遷移するようにはなったのですが、この時にブラウザ上で下記エラーが発生してしまいます。
どこを直せばいいのか、原因がわからない状態のため、何かお考えがあればご教示いただけると幸いです。なお、profile関係では、def createの中に一つだけredirect_toは入れ込んでいる状態です。
respond_to do |format| if @profile.save format.html { redirect_to "/authors/new", notice: 'Profile was successfully created.' } format.json { render :show, status: :created, location: @profile } else format.html { render :new } format.json { render json: @profile.errors, status: :unprocessable_entity } end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/27 13:52