我正在尝试按照本教程(Michael Hartl 的 Rails 教程)来创建有效用户。填写密码字段仍然给我错误“密码不能为空”和“密码必须更长,谢谢 6 个字母”。我尝试通过 rails 控制台创建用户,效果很好。只有当我尝试通过网站本身进行操作时。
我的用户模型如下所示:
class User < ApplicationRecord
#before saving, make sure the email is in downcase
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: {case_sensitive: false}
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
我的 user_controller 看起来像这样:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
#wrap_parameters :user, include: [:name, :email, :password, :password_confirmation]
def index
@users = User.all
end
def show
@user = User.find(params[:id])
#debugger
end
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
#POST /users
#POST /users.json
def create
@user = User.new(user_params) #params[:user])#user_params)
respond_to do |format|
if @user.save
#flash[:success] = "Welcome to CourseBuddies!"
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
redirect_to @user
else
#render 'new'
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email, :pasword, :password_confirmation)
end
end
这是我的迁移文件 create_user:
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password
t.string :password_confirmation
t.timestamps
end
end
结尾
我已经尝试过在网上找到的不同解决方案,比如使用“wrap_parameters”,但它似乎对我没有任何作用。
我的 Gemfile 看起来像这样:
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.1'
gem 'bootstrap-sass', '3.3.7'
# make pw digest "has_secure_password" use a stateofart hash function (bcrypt)
gem 'bcrypt', '3.1.11'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18.4'
# 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 'sqlite3', '1.3.13' #railsTuto said to get this, but already use postgresql
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
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
group :test do
gem 'rails-controller-testing', '1.0.2'
gem 'minitest-reporters', '1.1.14'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
end
group :production do
gem 'pg', '~>0.18.4'
gem 'rails_12factor'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
编辑:我的表格
<%- #FIELDS FOR THE FORM %>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: signup_path) do |f| %>
<%- #ERROR MESSAGES %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<br>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<br>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<br>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<br><br>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
我将不胜感激任何帮助或澄清。谢谢!