我是Ruby on Rails的新手,我正在尝试将我一直关注的一些教程付诸实践。
我目前正在努力让基本的用户注册工作。
当注册表单完成并提交后,Firstname和Lastname字段将被清空,我收到两条错误消息(运行黄瓜测试时也出现失败错误):
我认为我已经错过了使用身份验证代码相当明显的内容,但无法发现我错过的内容。
我的所有代码都在github account
上class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
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)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { render action: '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 { head :no_content }
else
format.html { render action: '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 }
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(:firstname, :middlename, :lastname, :email, :password, :password_confirmation)
end
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :middlename
validates :firstname, :presence =>true
validates :lastname, :presence=>true
validates :email, :presence=>true, :uniqueness => { :case_sensitive => false }
before_validation :downcase_email
def name
[firstname, middlename, lastname].compact.join(' ')
end
private
def downcase_email
self.email = self.email.downcase if self.email.present?
end
end
<h1>New user</h1>
<%= render 'form' %>
<%= link_to 'Back', users_path %>
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :firstname %><br />
<%= f.text_field :firstname %>
</div>
<div class="field">
<%= f.label :lastname %><br />
<%= f.text_field :lastname %>
</div>
<div class="field">
<%= f.label :middlename %><br />
<%= f.text_field :middlename %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
答案 0 :(得分:1)
由于看起来您正在使用设计,因此您可以尝试运行rails generate devise:views
。这将为设计的所有内容生成所有正确的视图。您可能希望保存已有的表单,以防Devise尝试覆盖它们。