前提・実現したいこと
お世話になっております。今回もよろしくおねがいします。
railsのユーザー新規登録でパラメーターがコントローラー側に渡っているのに、ユーザーの作成に失敗してしまいます。
コンソール上では保存が可能なのに、なぜ失敗してしまうのかわからず、困っています。
お知恵をお貸しいただければ幸いです。
###binding.pryのデータ
From: /home/ec2-user/environment/new_app2/app/controllers/users_controller.rb @ line 15 UsersController#create: 11: def create 12: @user=User.new(user_params) 13: if @user.save 14: binding.pry => 15: redirect_to user_path(@user) 16: else 17: 18: render new_user_path 19: end 20: end [1] pry(#<UsersController>)> @user => #<User id: 41, email: "testuser22@gmail.com", created_at: "2019-08-15 12:22:27", updated_at: "2019-08-15 12:22:27", admin_flg: nil, refresh_token: nil, access_token: nil, userimage: nil, username: "testuser"> [2] pry(#<UsersController>)> @user.save (0.1ms) begin transaction (0.0ms) commit transaction => true
app/views/users/new.html.hamlからsubmitしたときのターミナルログ
Started POST "/users" for 36.3.213.27 at 2019-08-15 12:18:41 +0000 Cannot render console from 36.3.213.27! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by UsersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"hnrNfvrGA6GCx9HqTyaPzoBW2Kvibj83lU8LSfiRmG71Jr6AwvmjUe/ 1nK2lmCBRE8teKZeuCVhKdZ0MagsjLA==", "user"=>{"username"=>"testuser", "email"=>"testuser@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"登録する"} (0.1ms) begin transaction User Create (0.5ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at", "username") VALUES (?, ?, ?, ?, ?) [["email", "testuser@gmail.com"], ["encrypted_password", "$2a$11$Z9AD6ras0Qra6q65mnR9EOsIpdzmfDUD4/hQgywLjQrymhN61lrX6"], ["created_at", "2019-08-15 12:18:41.330942"], ["updated_at", "2019-08-15 12:18:41.330942"], ["username", "testuser"]] (5.1ms) commit transaction Redirected to https://9ba3b08da6334d189d339fb602aa9537.vfs.cloud9.us-east-2.amazonaws.com/users/40 Completed 302 Found in 165ms (ActiveRecord: 5.7ms) Started GET "/users/40" for 36.3.213.27 at 2019-08-15 12:18:41 +0000 Cannot render console from 36.3.213.27! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by UsersController#show as HTML Parameters: {"id"=>"40"} Redirected to https://9ba3b08da6334d189d339fb602aa9537.vfs.cloud9.us-east-2.amazonaws.com/users/new Filter chain halted as :check_user_login? rendered or redirected Completed 302 Found in 2ms (ActiveRecord: 0.0ms) Started GET "/users/new" for 36.3.213.27 at 2019-08-15 12:18:41 +0000 Cannot render console from 36.3.213.27! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by UsersController#new as HTML Rendering users/new.html.haml within layouts/application Rendered users/new.html.haml within layouts/application (17.0ms) Rendered layouts/_head.html.haml (65.3ms) Rendered layouts/_header.html.haml (9.8ms) Rendered layouts/_flash.html.haml (1.5ms) Rendered layouts/_footer.html.haml (1.5ms) Completed 200 OK in 107ms (Views: 105.5ms | ActiveRecord: 0.0ms)
該当のソースコード
app/views/users/new.html.haml
%h2.page_title ユーザー登録ページ = simple_form_for(@user,html: {multipart: true}) do |f| .form-inputs.form_group = f.input :username, required: true, autofocus: true, input_html: { autocomplete: "username",class:"form-control" } = f.input :email, required: true, autofocus: true, input_html: { autocomplete: "email",class:"form-control"} = f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length), input_html: { autocomplete: "new-password",class:"form-control" } = f.input :password_confirmation, required: true, input_html: { autocomplete: "new-password",class:"form-control mb-3" } = f.input :user_image, as: :file = f.button :submit, "登録する", input_html: { class:" btn btn-primary " } = link_to 'Signin with Google',user_google_omniauth_authorize_path
app/controller/users_controllers
# frozen_string_literal: true class UsersController < ApplicationController before_action :check_user_login?, only: [:show] def new @user=User.new end def create @user=User.new(user_params) binding.pry if @user.save redirect_to user_path(@user) else render new_user_path end end def edit user_find_by_id end def update @user = User.find_by(user_params) if @user.update(user_params) redirect_to user_path(id: current_user.id) else render edit_user_path end end def destroy user_find_by_id @user.destroy end def show user_find_by_id @tours = Tour.where(user_id: @user.id) end def followings user_find_by_id @users = @user.followings.page(params[:page]) render 'follow' end def followers user_find_by_id @users = @user.followers.page(params[:page]) render 'follow' end def favorite @tours = current_user.favtours.page(params[:page]) end private def user_find_by_id @user = User.find_by(id: params[:id]) end def user_params params.require(:user).permit(:username,:email, :password, :password_confirmation, :user_image) end end
app/model/user.rb
# frozen_string_literal: true class User < ApplicationRecord has_many :tours, dependent: :destroy has_many :comments, dependent: :destroy has_many :likes, dependent: :destroy has_many :liked_tours, through: :likes, source: :tour has_many :favorites has_many :favtours, through: :favorites, source: :tour has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user devise :database_authenticatable, :recoverable, :rememberable, # :validatable, :registerable, :omniauthable mount_uploader :userimage, UserimageUploader
回答2件
あなたの回答
tips
プレビュー