railsチュートリアルに沿ってアプリを学習中のrails歴4ヶ月の初心者です。
ログイン機能について実装し、正常に動いていたのですが、チュートリアル13章の「ユーザ情報についてデータベースをリセットする。」という指示のあと、ログインできないため(ユーザがいないため)、新規でユーザーを登録しようとしたところ、新規ユーザー登録のための氏名、email、パスワードを入力すると、ページ遷移せずに、そのままパスワードの欄が空になり元の新規登録のページに戻ってしまうというエラー?が発生しています。
新規登録にあたるコードはいじっていないですし、railsチュートリアルに戻ってコードの確認をしてもコードに変更はないため、なぜこうなるのかわかりません。
エラー画面も、新規登録時のフラッシュ(パスワードが抜けています等)は一切発生していません。
以下にコードを記載します。
どなたか、似たような現象に遭遇した方、解決方法についてご助言いただける方教えていただけると幸いです。
・ユーザーモデル
userモデル
1class CreateUsers < ActiveRecord::Migration[5.0] 2 def change 3 create_table :users do |t| 4 t.string :name 5 t.string :email 6 7 t.timestamps 8 end 9 end 10end
・user.rb
user.rb
1# == Schema Information 2# 3# Table name: users 4# 5# id :integer not null, primary key 6# email :string 7# name :string 8# password_digest :string 9# remember_digest :string 10# created_at :datetime not null 11# updated_at :datetime not null 12# 13# Indexes 14# 15# index_users_on_email (email) UNIQUE 16# 17 18class User < ApplicationRecord 19 has_many:posts 20 before_save { email.downcase! } 21 validates :name, presence: true, length: { maximum: 50 } 22 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i 23 validates :email, presence: true, length: { maximum: 255 }, 24 format: { with: VALID_EMAIL_REGEX }, 25 uniqueness: { case_sensitive: false } 26 has_secure_password 27 validates :password, presence: true, length: { minimum: 6 } 28 29 # 渡された文字列のハッシュ値を返す 30 def self.digest(string) 31 cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 32 BCrypt::Engine.cost 33 BCrypt::Password.create(string, cost: cost) 34 end 35 36# ランダムなトークンを返す 37 def self.new_token 38 SecureRandom.urlsafe_base64 39 end 40 41# 永続セッションのためにユーザーをデータベースに記憶する 42 def remember 43 self.remember_token = User.new_token 44 update_attribute(:remember_digest, User.digest(remember_token)) 45 end 46 47# 渡されたトークンがダイジェストと一致したらtrueを返す 48 def authenticated?(remember_token) 49 return false if remember_digest.nil? 50 BCrypt::Password.new(remember_digest).is_password?(remember_token) 51 end 52 53 # ユーザーのログイン情報を破棄する 54 def forget 55 update_attribute(:remember_digest, nil) 56 end 57 58 belongs_to :user 59 60 #favorite機能をつけた時に追加 61 has_many :favorites, through: :favorites, source: :post, dependent: :destroy 62 has_many :posts, through: :favorites 63 has_many :posts, dependent: :destroy 64end 65 66
・users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) @user = User.find(params[:id]) @posts = @user.posts.paginate(page: params[:page]) # モデルクラスのwhereメソッドへ検索カラムと値を渡すと合致するモデルを配列形式で返却 #@posts = Post.where(:user_id, @user.id) end def new @user = User.new end def create @user = User.new(user_params) if @user.save log_in @user # 保存の成功をここで扱う。 flash[:success] = "Tsukutta!へようこそ" redirect_to @user else render 'new' end end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end end
以上です。掲載していないコードなどがありましたらご指摘ください。
よろしくお願いします。
・追記
db/schema.rbの中身はこのようになっています!
# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 20181016133724) do create_table "comments", force: :cascade do |t| t.integer "post_id" t.string "name", null: false t.text "comment", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["post_id"], name: "index_comments_on_post_id" end create_table "favorites", force: :cascade do |t| t.integer "user_id", null: false t.integer "post_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["post_id"], name: "index_favorites_on_post_id" t.index ["user_id"], name: "index_favorites_on_user_id" end create_table "post_tag_relations", force: :cascade do |t| t.integer "post_id" t.integer "tag_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["post_id"], name: "index_post_tag_relations_on_post_id" t.index ["tag_id"], name: "index_post_tag_relations_on_tag_id" end create_table "posts", force: :cascade do |t| t.string "name" t.string "title" t.text "content" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "picture" t.index ["user_id", "created_at"], name: "index_posts_on_user_id_and_created_at" t.index ["user_id"], name: "index_posts_on_user_id" end create_table "tags", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "name" t.string "email" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "password_digest" t.string "remember_digest" t.index ["email"], name: "index_users_on_email", unique: true end end
・Gemfileを追加。bcryptがあることを確認しました。
source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.0', '>= 5.0.0.1' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use Puma as the app server gem 'puma', '~> 3.0' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 3.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development, :test do gem 'rspec-rails', '3.1.0' # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri # 追加 gem 'rails-flog', require: 'flog' end group :development do # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. gem 'web-console' gem 'listen', '~> 3.0.5' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'bootstrap-sass', '3.3.7' gem 'data-confirm-modal' gem 'kaminari' gem 'rails-i18n' gem 'annotate' gem 'rake', '< 11.0' gem 'carrierwave' gem 'bcrypt', '3.1.12' gem 'font-awesome-rails'
・users/new.html.erbを追加しました!!
<% provide(:title, 'Sign up') %> <h1>無料会員登録</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= form_for(@user, url: signup_path) do |f| %> <%= render 'shared/error_messages' %> <%= f.label :name %> <%= f.text_field :name, class: 'form-control' %> <%= f.label :email %> <%= f.email_field :email, class: 'form-control' %> <%= f.label :password %> <%= f.password_field :password, class: 'form-control' %> <%= f.label :password_confirmation, "Confirmation" %> <%= f.password_field :password_confirmation, class: 'form-control' %> <%= f.submit "アカウントを作成する", class: "btn btn-primary" %> <% end %> </div> </div>
新規登録後、戻されるページのURLは以下のとおりです。登録前と変わらないところに戻されます!
https://b1fcca702fd243a2b259db29bd0cfdd6.vfs.cloud9.ap-southeast-1.amazonaws.com/signup
・routes.rbを追加します!
Rails.application.routes.draw do get 'sessions/new' get 'static_pages/home' get '/about', to: 'static_pages#about' get '/contact', to: 'static_pages#contact' get '/signup', to: 'users#new' post '/signup', to: 'users#create' resources :users get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' get 'comments/create' get 'comments/destroy' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root 'static_pages#home' resources :favorites, only: %i[create destroy] resources :comments, only: %i[create destroy] resources :posts end
回答1件
あなたの回答
tips
プレビュー