railsで下記のコードでサイトのどのページに行ってもログイン無しでユーザー情報を生成、cookieに保存してユーザーの判別をしたいと思っています。
cookieに保存されるところまではできているんですがユーザー情報をDBにうまく保存されないです。.saveの当たりがうまく行ってなさそうなんですがよく理由が分からず。。。教えてもらえると助かります。
application_controller.rb
ruby
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 before_action :setting, :createlogin 4 def setting 5 @site_name = "LOOPLOOP" 6 end 7 def createlogin 8 if session[:user_id] == User.find_by(id:session[:id]) 9 @current_user = User.find_by(id:session[:id]) 10 @current_user.save 11 else 12 @current_icon_id = rand(10) 13 @current_icon_color_id = rand(10) 14 @current_user = User.new(icon_id: @current_icon_id,icon_back_id: @current_icon_color_id) 15 @current_user.save 16 session[:user_id] = @current_user.id 17 end 18 end 19end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/09 23:53