如何在ROR中解决空白密码字段问题

时间:2015-01-03 06:00:39

标签: ruby-on-rails-3 sqlite ruby-on-rails-4

任何人都可以解决我的问题。我想在sqlite数据库中保存我的所有值。我有名字,emailid和密码字段。当我点击提交按钮时,只有名称和电子邮件ID存储在数据库中我发现密码字段显示为空白。我使用的是rails版本4.0.2和ruby 1.9.3。代码如下所示。

视图/用户/ index.html.erb:

<h1>This is index page</h1>
<center>
<p>Enter data</p>
 <div class="option">
   <p><%= link_to "Click here to enter data",users_new_path %></p>
   <p><%= link_to "Display data",users_display_path%></p>
 </div>
 </center>

视图/用户/ new.html.erb:

<h1>Enter your data here</h1>
<center>
  <%= form_for @user ,:url => {:action => "create"} do |f| %>
  <div class="div_reg">
    <p>
      <label for="username" class="uname" data-icon="u" >username </label>&nbsp;
      <%= f.text_field:name,placeholder:"Enter your user name" %>
    </p>
    <p>
      <label for="username" class="uname" data-icon="u" >Email </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <%= f.text_field:email,placeholder:"enter your email" %>
    </p>
    <p>
      <label for="username" class="uname" data-icon="u" >Password </label>&nbsp;&nbsp;
      <%= f.password_field:password,placeholder:"Enteryour password" %>
    </p>
    <center>
      <%= f.submit "Submit",:class => 'btn-custom' %>
    </center>
    <div class="back_btn">
      <a href="/users/index"><button type="button" class="btn-custom " style="cursor:pointer;">Back</button></a>
    </div>
  </div>
      <% end %>
</center>
<% if @user.errors.any? %>
    <ul class="Signup_Errors">
      <% for message_error in @user.errors.full_messages %>
          <li><%= message_error %></li>
      <% end %>
    </ul>
<% end %>

控制器/ users_controller.rb:

class UsersController < ApplicationController
  def index

  end
  def new
@user=User.new
  end
  def create
@user=User.new(users_param);
    if @user.save
 flash[:notice]="You signed up successfully"
      flash[:color]="valid"
      redirect_to :action => 'index'
    else
      flash[:alert]="You have not signed up successfully"
      flash[:color]="invalid"
      redirect_to :action => 'new'
    end
  end
  private
  def users_param
    params.require(:user).permit(:name, :email, :password)
  end
def display

end

end

模型/ user.rb:

class User < ActiveRecord::Base
  attr_accessor :password
  EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
  validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :password, :confirmation => true
  validates_length_of :password, :in => 6..20, :on => :create
end

迁移\ 20150102052336_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :password

      t.timestamps null: false
    end
  end
end

请帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

您应该添加password_confirmation字段

<%= f.password_field :password %> 
<%= f.password_field> :password_confirmation %>

add也将其添加到允许的参数

params.require(:user).permit(:name, :email, :password, :password_confirmation)

PS:一旦你想出了如何创建自己的注册/登录实现,我建议你查找一下devise gem。