3

我是一名 Ruby on Rails 新手,并且正在尝试将我一直遵循的一些教程付诸实践。

我目前正在尝试让基本的用户注册工作。

完成并提交注册表单后,Firstname 和 Lastname 字段被空白,我收到两条错误消息(在运行 cucumber 测试时我也收到一个失败错误):

  • 名字不能为空
  • 姓氏不能为空

我想我错过了一些相当明显的身份验证代码,但无法发现我错过了什么。

我所有的代码都在我的github 帐户上

控制器

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

查看用户

新的erb.rb

<h1>New user</h1>

<%= render 'form' %>

<%= link_to 'Back', users_path %>

_form.rb

<%= 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 %>
4

1 回答 1

1

由于看起来您正在使用设计,您可以尝试运行rails generate devise:views. 这将为设计所做的一切生成所有正确的视图。不过,您可能希望保存已有的表单,以防 Devise 尝试覆盖它们。

于 2014-02-05T18:49:45.567 回答