mercredi 11 mars 2015

How the data will saved in SQlite DB using Ruby on rails

I have a issue regarding save datas in database using RoR. Can anybody please help me to solve the issue which is explained below.


Issue:


Actually I want to save data in DB after clicking the submit button.But when I clicked on submit button it is showing message "data could not submitted successfully".


My code snippets are as follows.


views/homes/registration.html.erb:



<div class="totaldiv">
<div class="navdiv"><span>STUDENT INFORMATION</span></div>
<div class="wrapper">
<div id="leftsidebtn">
<ul>
<li><a href="/homes/index">Back</a></li>
</ul>
</div>
</div>
<div class="restdiv" id="ex3" >
<center>
<div class="reg_form">
<%= form_for :users,:url => {:action => 'savedata'} do |f| %>
<p>
<label for="first_name">First Name :</label>
<%= f.text_field :first_name,:class => 'first_name', placeholder:"Enter your first name" %>
</p>
<p>
<label for="last_name">Last Name :</label>
<%= f.text_field :last_name,:class => 'first_name', placeholder:"Enter your last name" %>
</p>
<p>
<label for="email_id">Email Id :</label>
<%= f.email_field :email,:class => 'first_name', placeholder:"Enter your email id" %>
</p>
<p>
<label for="tel_no">Phone No :</label>
<%= f.telephone_field :tel_no,:class => 'first_name', placeholder:"Enter your Phone no" %>
</p>
<p>
<label for="tel_no">Password :</label>
<%= f.password_field :password,:class => 'first_name', placeholder:"Enter your password" %>
</p>
<p>
<label for="tel_no">Confirm password</label>
<%= f.password_field :password_confirmation ,:class => 'first_name', placeholder:"Enter your password again" %>
</p>
<p>
<label for="adress">Adress :</label>
<%= f.text_area :address ,:class => 'address_text', placeholder:"Enter your Adress" %>
</p>
<p>
<%= f.submit 'Submit Data',:class => 'submit_button' %>
</p>
<% end %>
</div>
</center>
</div>
</div>


controller/homes_controller.rb:



class HomesController < ApplicationController
before_filter :authenticate_admin!,only: [:admin]
def index

end
def admin

end
def managebooks
@books=Book.new
if params[:id]
@books=Book.find(params[:id])
@book=Book.all
end
end
def savebooks
@books=Book.new(params[:books])
if @books.save
flash[:notice]="Data has submitted successfully"
flash[:color]="valid"
redirect_to :action => 'managebooks',:id => @books.id
else
flash[:notice]="Data couldnot submitted successfully"
flash[:color]="invalid"
render 'managebooks'
end
end
def remove
@books=Book.find(params[:id])
@books.destroy
end
def books

end
def showbooks
@books=Book.all
end
def searchbooks
@books=Book.all
end
def member

end
def registration
@users=User.new
end
def savedata
@users=User.new(params[:users])
if @users.save
flash[:notice]="Data has submitted successfully"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:notice]="Data could not submitted successfully"
flash[:color]="invalid"
render 'registration'
end
end
end


model/users.rb



class User < ActiveRecord::Base
attr_accessible :address, :email, :first_name, :last_name, :password, :password_hash, :password_salt, :tel_no ,:password_confirmation
attr_accessor :password
before_save :encrypt_password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :first_name, :presence => true, :length => {:in => 3..8}
validates :last_name , :presence => true , :length => {:in => 3..10}
validates :tel_no , :presence => true , :length => {:in => 1..10}
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end

def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end


db/migrate/20150311112733_create_users.rb



class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :password
t.string :tel_no
t.string :address
t.string :password_hash
t.string :password_salt

t.timestamps
end
end
end


Please check my above codes and try to help me to resolve my issue.I am using rails version-3.2.19 and ruby version-1.9.3.Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire