Can anybody resolve my one issue.I want to save all of my values in sqlite database.I have name,emailid and password field.when i clicked on submit button only name and email-id has stored in database bit i found that password filed is showing blank.I am using rails version-4.0.2 and ruby 1.9.3.The codes are given below.
views/users/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>
views/users/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>
<%= f.text_field:name,placeholder:"Enter your user name" %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Email </label>
<%= f.text_field:email,placeholder:"enter your email" %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= 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 %>
controller/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
model/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
migrate\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
Please help me to resolve this issue.Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire