前提・実現したいこと
Railsでwebアプリを作っています。
detail_user_pathで、Userモデルにおけるユーザーの詳細情報に関わるカラムをnil => (内容)に更新する機能を作りたいのですが、ソースコードで実行してもなぜか@userがinvalidになってしまいます。(password cant be blankのエラーメッセージがコンソールに出ました)
update_attributeも使っていますし、user_update_paramsも用意したのですが、何がダメなのか全くわからないです。。。
私のコードの修正すべき点を教えていただきたいです。
程度の低い質問でしたら申し訳ございません。
また、質問に必要な情報等に不足があれば、お手数ですがご指摘いただければ、追記いたします。
よろしくお願いいたします。
該当のソースコード
ruby
1(コントローラ) 2class UsersController < ApplicationController 3 #before_action :logged_in_user, only: [:edit, :update, :destroy, :following, :followers] 4 before_action :correct_user, only: [:edit, :update, :destroy] 5 6 def show 7 @user = User.find(params[:id]) 8 #@microposts = @user.microposts.paginate(page: params[:page]) 9 end 10 11 def new 12 @user = User.new 13 end 14 15 def signup_jp 16 @user = User.new 17 end 18 19 def after_signup 20 21 end 22 23 def create 24 @user = User.new(user_params) 25 if @user.save 26 @user.send_activation_email 27 log_in @user 28 flash[:info] = "Please check your email to activate your account." 29 # redirect_to after_signup_path 30 #redirect_to new_detail_path 31 # TODO: ここでユーザー認証を促す画面にリダイレクトさせるようにあとで設定する 32 # redirect_to root_url 33 redirect_to root_path 34 else 35 render 'new' 36 end 37 end 38 39 def detail 40 @user = User.find(params[:id]) 41 end 42 43 def detailcreate 44 @user = User.find_by(id: params[:id]) 45 #binding.pry 46 if @user.update_attribute(user_update_params) 47 redirect_to @user 48 else 49 flash[:danger] = "Invalid information is included." 50 render 'detail' 51 end 52 end 53 54 55 56 def edit 57 @user = User.find(params[:id]) 58 @detail = Detail.find_by(user_id: @user.id ) 59 end 60 61 def update 62 @user = User.find_by(id: params[:id]) 63 #binding.pry 64 if @user.update_attribute(user_update_params) 65 redirect_to @user 66 else 67 flash[:danger] = "Invalid information is included." 68 render 'edit' 69 end 70 end 71 72 73 # def edit_email edit_emailcontrollerに移行した 74 # @user = User.find(params[:id]) 75 # end 76 77 78 def edit_password 79 @user = User.find(params[:id]) 80 end 81 82 83 84 85 private 86 87 def user_params #strong parameterのネスト化実装 88 params.require(:user).permit( 89 :name, 90 :email, 91 :image, 92 :password, 93 :password_confirmation, 94 :mother_tongue, 95 :japanese_level, 96 :english_level, 97 :gender, 98 :region, 99 :purpose, 100 :self_introduction, 101 :skype, 102 :discord, 103 :other#, 104 # :authenticity_token 105 ) 106 end 107 # .merge(user_id: User.find_by(id: params[:id]).id) #これはつけた方がいいですか? 108 109 def user_update_params 110 params.permit( 111 :mother_tongue, 112 :japanese_level, 113 :english_level, 114 :gender, 115 :region, 116 :purpose, 117 :self_introduction, 118 :skype, 119 :discord, 120 :other 121 ) 122 end 123 124 125 126 def correct_user 127 @user = User.find(params[:id]) 128 redirect_to(root_url) unless current_user?(@user) 129 end 130 131end
html
1<% provide(:title, "user detail") %> 2 3<div class="user-detail-wrapper"> 4 <div class="introduction"> 5 <h3>①Authorize your account via sent Email.</h3> 6 <h3>②Let's register your user informations! You can change it later.</h3> 7 </div> 8 9 <div class="row user-detail-forms"> 10 <div class="col-md-6 col-md-offset-3"> 11 <%= form_with(url: detailcreate_user_path, method: "patch", local: true) do |f| %> 12 13 14 <%= f.label :mother_tongue, "Mother Tongue" %> 15 <%= f.select :mother_tongue, [["English", "English"], ["Japanese", "Japanese"], 16 ["Other","other"]], include_blank: "Please select.", class:"form-control" %> 17 18 <%= f.label :japanese_level, "Japanese Level" %> 19 <%= f.select :japanese_level, [["Beginner", "Beginner"], ["Capable of quite basic communication", "Capable of quite basic communication"], 20 ["Capable of daily conversation | 日常会話ができる","Capable of daily conversation | 日常会話ができる"], ["Capable of advanced conversation | 難しい内容の会話ができる","Capable of advanced conversation | 難しい内容の会話ができる"], ["Almost native level | ネイティブとほとんど同レベル","Almost native level | ネイティブとほとんど同レベル"], 21 ["Native | ネイティブ","Native | ネイティブ"]], include_blank: "Please select.", class:"form-control" %> 22 23 <%= f.label :english_level, "English Level | 英語のレベル" %> 24 <%= f.select :english_level, [["Beginner | 初心者", "Beginner | 初心者"], ["Capable of quite basic communication | 簡単な会話ならできる", "Capable of quite basic communication | 簡単な会話ならできる"], 25 ["Capable of daily conversation | 日常会話ができる","Capable of daily conversation | 日常会話ができる"], ["Capable of advanced conversation | 難しい内容の会話ができる","Capable of advanced conversation | 難しい内容の会話ができる"], ["Almost native level | ネイティブとほとんど同レベル","Almost native level | ネイティブとほとんど同レベル"], 26 ["Native | ネイティブ","Native | ネイティブ"]], include_blank: "Please select.", class:"form-control" %> 27 28 29 30 <%= f.label :self_introduction, "Self Introduction | 自己紹介" %> 31 <%= f.text_area :self_introduction, class: 'form-control self-intro', placeholder: "It is recommended that you write in simple English. 英語で書くことをお勧めします。簡単な文でも問題ありません。" %> 32 33 <%= f.label :skype, "URL of Your SNS 1 (optional) | SNSリンク1(任意)" %> 34 <%= f.text_field :skype, class: 'form-control' %> 35 36 <%= f.label :discord, "URL of Your SNS 2 (optional) | SNSリンク2(任意)" %> 37 <%= f.text_field :discord, class: 'form-control' %> 38 39 <%= f.label :other, "URL of Your SNS 3 (optional) | SNSリンク3(任意)" %> 40 <%= f.text_field :other, class: 'form-control' %> 41 42 <%= f.text_field :name, type: "hidden", value: "#{@user.name}" %> 43 <%= f.text_field :email, type: "hidden", value: "#{@user.email}" %> 44 45 46 47 48 <%= f.submit "Send | 送信", class: "btn btn-primary create" %> 49 <% end %> 50 </div> 51 </div> 52</div>
ruby
1(user.rb) 2class User < ApplicationRecord 3 4 attr_accessor :remember_token, :activation_token, :reset_token 5 before_save :downcase_email 6 before_create :create_activation_digest 7 validates :name, presence: true, length: { maximum: 25 } 8 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 9 validates :email, presence: true, length: { maximum: 255 }, 10 format: { with: VALID_EMAIL_REGEX }, uniqueness: true 11 has_secure_password 12 validates :password, presence: true, length: { minimum: 6 } 13 mount_uploader :image, ImageUploader 14 15 # 渡された文字列のハッシュ値を返す 16 def User.digest(string) 17 cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 18 BCrypt::Engine.cost 19 BCrypt::Password.create(string, cost: cost) 20 end 21 22 def User.new_token 23 SecureRandom.urlsafe_base64 24 end 25 26 # 永続セッションのためにユーザーをデータベースに記憶する 27 def remember 28 self.remember_token = User.new_token 29 update_attribute(:remember_digest, User.digest(remember_token)) 30 end 31 32 # 渡されたトークンがダイジェストと一致したらtrueを返す 33 def authenticated?(attribute, token) 34 digest = send("#{attribute}_digest") 35 return false if digest.nil? 36 BCrypt::Password.new(digest).is_password?(token) 37 end 38 39 def forget 40 update_attribute(:remember_digest, nil) 41 end 42 43 # アカウントを有効にする 44 def activate 45 update_attribute(:activated, true) 46 update_attribute(:activated_at, Time.zone.now) 47 end 48 49 # 有効化用のメールを送信する 50 def send_activation_email 51 UserMailer.account_activation(self).deliver_now 52 end 53 54 # パスワード再設定の属性を設定する 55 def create_reset_digest 56 self.reset_token = User.new_token 57 update_attribute(:reset_digest, User.digest(reset_token)) 58 update_attribute(:reset_sent_at, Time.zone.now) 59 end 60 61 # パスワード再設定のメールを送信する 62 def send_password_reset_email 63 UserMailer.password_reset(self).deliver_now 64 end 65 66 def password_reset_expired? 67 reset_sent_at < 2.hours.ago 68 end 69 70 private 71 72 def downcase_email 73 self.email = email.downcase 74 end 75 76 def create_activation_digest 77 self.activation_token = User.new_token 78 self.activation_digest = User.digest(activation_token) 79 end 80 81 82end 83
ruby
1(routes.rb) 2Rails.application.routes.draw do 3 get 'password_resets/new' 4 get 'password_resets/edit' 5 get 'sessions/new' 6 root 'static_pages#index' 7 resources :static_pages, only: [:index] 8 resources :users do 9 member do 10 get 'detail' 11 patch 'detailcreate' 12 end 13 end 14 resources :sessions, only: [:index, :create, :destroy] 15 resources :account_activations, only: [:edit] 16 resources :password_resets, only: [:new, :create, :edit, :update] 17end 18
補足情報(FW/ツールのバージョンなど)
Rails 6.0.3
Ruby 2.6.3
回答1件
あなたの回答
tips
プレビュー