How to solve Template is missing error using Rails 3? - ruby

I am getting the below error while my page is failed to lagged in.If i am typing wrong password/email and clicked on submit then the bellow error is coming.
Error:
Template is missing
Missing template sessions/member, application/member with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/Site/library_management1/app/views" * "C:/Ruby193/lib/ruby/gems/1.9.1/gems/devise-3.4.1/app/views"
My codes are as follows.
views/homes/member.html.erb
<% if current_user %>
<div class="totaldiv">
<div class="navdiv"><span>STUDENT INFORMATION</span><span>Logged in as <%= current_user.email %></span></div>
<div class="wrapper">
<div id="leftsidebtn">
<ul>
<li>Book issue</li>
<li>Books Available</li>
<li>Log Out</li>
</ul>
</div>
</div>
<div class="restdiv" id="ex3" >
<center>
</center>
</div>
</div>
<% else %>
<div class="totaldiv">
<div class="navdiv"><span>STUDENT INFORMATION</span></div>
<div class="wrapper">
<div id="leftsidebtn">
<ul>
<li>Registration</li>
<li>Back</li>
</ul>
</div>
</div>
<div class="restdiv" id="ex3" >
<center>
<div class="studentlogin">
<h1>Login Here</h1>
<section class="studentloginloginform cf">
<%= form_for :users,:url => {:action => 'loginuser',:controller => 'sessions'} do |f| %>
<% if #users.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#users.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #users.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<ul>
<li>
<label for="usermail">Email</label>
<%= f.email_field :email,placeholder:"yourname#email.com" %>
</li>
<li>
<label for="password">Password</label>
<%= f.password_field :password,placeholder:"password" %>
</li>
<li>
<%= f.submit 'LogIn',:class => 'studentsubmit' %>
</li>
<li class="reg_member">
Not a member ? <%= link_to 'Register Here',homes_registration_path %>
</li>
</ul>
<% end %>
</section>
</div>
</center>
</div>
</div>
<% end %>
controller/sessions_controller.rb
class SessionsController < ApplicationController
def loginuser
#users=User.authenticate(params[:users][:email], params[:users][:password])
if #users
session[:user_id]=#users.id
cookies.signed[:user_id]=#users.id
flash[:notice]="login successfull"
flash[:color]="valid"
redirect_to :action => 'member',:controller => 'homes'
else
flash[:notice]="could not Logged in"
flash[:color]="invalid"
render 'member', :controller => 'homes'
end
end
def removeuser
session[:user_id] = nil
cookies.delete :user_id
flash[:notice]="user logged out successfully"
flash[:color]="valid"
redirect_to :action => 'member', :controller => 'homes'
end
end
model/user.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..10}
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
has_many :issue
has_many :book
end
Please check the codes and try to resolve this error.

In your controller change line render 'member', :controller => 'homes' with following.
render 'homes/member'

You template is missing on "sessions/member" path
I guess, create a template in following path to resolve it
views/sessions/member.html.erb

Related

unable to upload images using paperclip gem

In my rails app I have a customer form of two steps.In step one customer enters his details and logo and pdf and in the next step he selects a theme which he like.
my customers_controller
def new
if current_user.admin?
#customer = Customer.new
#customer.additional_fields.build
else
redirect_to root_path, notice: 'You dont have admin permissions'
end
end
def edit
if current_user.admin?
#customer = Customer.find(params[:id])
else
redirect_to root_path, notice: 'You dont have admin permissions'
end
end
def theme_selector
#customer_id = params[:customer_id]
puts #customer_id
end
def create
Rails.logger.debug "PARAMS:"
params.each do |k,v|
Rails.logger.debug "#{k}: #{v}"
end
if current_user.admin?
#customer = Customer.new(customer_params)
respond_to do |format|
if #customer.save
format.html do
redirect_to #customer, notice: 'Customer was successfully created.'
end
format.json { render :show, status: :created, location: #customer }
else
format.html { render :new }
format.json do
render json: #customer.errors, status: :unprocessable_entity
end
end
end
else
redirect_to root_path, notice: 'You dont have admin permissions'
end
end
_form.html.erb
<%= form_for #customer, url: theme_selector_path,:method => :post,html: { multipart: true } do |f| %>
<input type="hidden" name="customer_id" value="<%= #customer.id %>">
<% if #customer.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#{pluralize(#customer.errors.count, "error")} prohibited this customer from being saved:" %>
</h2>
<ul>
<% #customer.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :address %>
<%= f.text_field :address, class:"form-control" %>
</div>
<div class="form-group">
<%= f.label :affiliate_id %>
<%= f.text_field :affiliate_id, class:"form-control" %>
</div>
<div class="control-group">
<%= f.label :logo, :class => 'control-label' %>
<div class="controls">
<%= f.file_field :logo,accept: 'image/png,image/gif,image/jpeg, image/jpg' %>
</div>
</div>
<div class="form-group">
<label>Upload File</label>
<%= f.file_field :file,accept: 'application/pdf' %>
</div>
<div class="form-group">
<% f.fields_for :additional_fields do | ing | %>
<%= ing.text_field :key, :size => 50 %>
<%= ing.text_field :value, :size => 50 %>
<% end %>
</div>
<div class="actions">
<div class="row">
<div class="col-md-5">
<span><%= f.submit 'Save', class: "btn-addmore" %></span>
<span><!--%= link_to 'Back', customers_path, class: "btn btn-danger" %--> </span>
</div>
<div class="col-md-2 col-md-offset-4">
<%= link_to "next", theme_selector_path, class: "btn-update" %>
</div>
</div>
</div>
<% end %>
I have a submit button at theme_selector.html.erb to create the customer.But I was getting an error "Paperclip::AdapterRegistry::NoHandlerError (No handler found for "#")" in customers controller create action.
development log:
Processing by CustomersController#create as HTML
Parameters: {"customer"=>{"name"=>"test", "address"=>"test", "affiliate_id"=>"test", "category"=>"", "domain"=>"test#web.com", "phone"=>"1111111111", "contact"=>"1214521", "email"=>"test#malibu.com", "comments"=>"none", "logo"=>"# <ActionDispatch::Http::UploadedFile:0x007f3f4946c258>", "file"=>"#<ActionDispatch::Http::UploadedFile:0x007f3f4946c208>"}}
PARAMS:
customer: #<ActionController::Parameters:0x007f3f442c0058>
controller: customers
action: create
Completed 500 Internal Server Error in 10ms (ActiveRecord: 1.0ms)
Paperclip::AdapterRegistry::NoHandlerError (No handler found for "# <ActionDispatch::Http::UploadedFile:0x007f3f4946c258>"):
app/controllers/customers_controller.rb:47:in `create'
Thanks in Advance!
The step-1 of customer form contains customer details and step-2 contains theme selection.However I was able to upload and save the data in step-1 but I was getting the error while saving the data from step-1 and step-2 together at the theme_selector page .
customer.rb
class Customer < ApplicationRecord
has_attached_file :logo
validates_attachment_content_type :logo, :content_type => /\Aimage\/.*\Z/
has_attached_file :file
validates_attachment_content_type :file, :content_type => "application/pdf"
end

Create Related Data using Nested attributes

api. Now need to insert a record which has associated records. I read nested attributes but didn't get any idea. I can't understand well.
I tried with one example in rails mvc project but even i can't create associated record in one-one mapping itself. Please guid me what the steps to do this or any good articles with sample?
Anyway the following is my workings
My model is
class Instructor < ActiveRecord::Base
has_one :office_assignment
has_many :departments
has_and_belongs_to_many :courses
accepts_nested_attributes_for :office_assignment
end
class OfficeAssignment < ActiveRecord::Base
belongs_to :instructor
end
My controller methods for create
def new
#instructor = Instructor.new
end
def create
#instructor = Instructor.new(instructor_params)
respond_to do |format|
if #instructor.save
format.html { redirect_to #instructor, notice: 'Instructor was successfully created.' }
format.json { render :show, status: :created, location: #instructor }
else
format.html { render :new }
format.json { render json: #instructor.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_instructor
#instructor = Instructor.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def instructor_params
params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, office_assignment_attributes: [:Location])
end
My create form
<%= form_for #instructor, :html => { :class => "form-horizontal instructor" } do |f| %>
<% if #instructor.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(#instructor.errors.count, "error") %> prohibited this instructor from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% #instructor.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<div class="form-group">
<%= f.label :LastName, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :LastName, :class => 'form-control' %>
</div>
<%= error_span(#instructor[:LastName]) %>
</div>
<div class="form-group">
<%= f.label :FirstMidName, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :FirstMidName, :class => 'form-control' %>
</div>
<%= error_span(#instructor[:FirstMidName]) %>
</div>
<div class="form-group">
<%= f.label :HireDate, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :HireDate, :class => 'form-control' %>
</div>
<%= error_span(#instructor[:HireDate]) %>
</div>
<div class="form-group">
<%= f.label :Location, :class => 'control-label' %>
<div class="controls">
<input type="text" name="office_assignment_Location" class ="form-control">
</div>
<%= error_span(#instructor[:HireDate]) %>
</div>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
instructors_path, :class => 'btn btn-default' %>
<% end %>
Please guide me..
Edit:
Changes in html file
<%= f.fields_for :office_assignment_Location do |loc| %>
<div class="form-group">
<%= loc.label :Location, :class => 'control-label' %>
<div class="controls">
<%= loc.text_field :Location %>
</div>
</div>
<%= loc.link_to_add "Add Location", :office_assignment_Location , :class=>'btn btn-primary'%>
<% end %>
My param in controller
def instructor_params
params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, office_assignment_attributes: [:id, :Location ,:_destroy])
end
My schema
create_table "instructors", force: :cascade do |t|
t.string "LastName", limit: 255
t.string "FirstMidName", limit: 255
t.date "HireDate"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "office_assignments", primary_key: "Instructor_Id", force: :cascade do |t|
t.string "Location", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_foreign_key "office_assignments", "instructors", column: "Instructor_Id"
FYI: attribute's name should start with small letter not capital.
Here are some changes:
class Instructor < ActiveRecord::Base
has_one :office_assignment , :dependent => :destroy # add dependent destroy
has_many :departments
has_and_belongs_to_many :courses
accepts_nested_attributes_for :office_assignment, :reject_if => :all_blank, :allow_destroy => true # add allow destroy
end
Some change in strong Parameters:
def instructor_params
params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, office_assignment_attributes: [:id, :Lesson ,:_destroy])
end
Note: Don't forget to add :id and :_destroy in office_assignment_attributes
Now in View(form_template):
Instead of this:
<div class="form-group">
<%= f.label :Location, :class => 'control-label' %>
<div class="controls">
<input type="text" name="office_assignment_Location" class ="form-control">
</div>
<%= error_span(#instructor[:HireDate]) %>
</div>
It should be:
<%= nested_form_for #instructor, :html => { :class => "form-horizontal instructor" } do |f| %>
......
.....
<%= f.fields_for :office_assignment do |loc| %>
<div class="form-group">
<%= loc.label :Location, :class => 'control-label' %>
<div class="controls">
<%= loc.text_field :Location %>
</div>
</div>
<% end %>
<%= f.link_to_add "Add Location", :office_assignment , :class=>'btn btn-primary'%>
You have to create nested form like this. I hope this helps you.
Note: Use nested_form_for instead of form_for in your form:
For your reference:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

Data could not updated in database using Rails 3?

I have a issue regarding update data using Rails 3.When user is editing profile and click on update button to update data,it could not update.I am explaining my codes below.
views/homes/userprofile.html.erb
<% if current_user %>
<div class="totaldiv">
<div class="navdiv"><span>STUDENT INFORMATION</span><span>Logged in as <%= current_user.email %></span></div>
<div class="wrapper">
<div id="leftsidebtn">
<ul>
<li>Book issue</li>
<li>Books Available</li>
<li>Magazines Purchase</li>
<li>Newspaper Purchase</li>
<li>Profile settings</li>
<li>My Blog</li>
<li>Log Out</li>
</ul>
</div>
</div>
<div class="restdiv" id="ex3" >
<center>
<div class="edit-profile"><button type="button" class="btn btn-success" id="btnShowModal" >Edit Your Profile</button></div>
<div id="output"></div>
<div id="overlay" class="web_dialog_overlay"></div>
<div id="dialog" class="web_dialog">
<div class="edit-firstdiv">
<div class="web_dialog_title align_right">
Close
</div>
<%= form_for :users,:url => {:action => 'updatedata',:id => params[:id] } do |f| %>
<% if #users.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#users.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #users.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="edit-firstname">
<label for="first_name">First Name</label>
<%= f.text_field :first_name,:id => 'first_name',:value => #users.first_name %>
</div>
<div class="edit-lastname">
<label for="last_name">Last Name</label>
<%= f.text_field :last_name,:id => 'first_name',:value => #users.last_name %>
</div>
<div class="edit-emailid">
<label for="emailid">Emailid</label>
<%= f.email_field :email,:id => 'first_name',:value => #users.email %>
</div>
<div class="edit-password">
<label for="password">Password</label>
<%= f.password_field :password,:id => 'first_name',:value => #users.password %>
</div>
<div class="edit-confirm">
<label for="Address">Password again</label>
<%= f.password_field :password_confirmation,:id => 'first_name' %>
</div>
<div class="edit-telephone">
<label for="phone">Phone no</label>
<%= f.telephone_field :tel_no,:id => 'first_name',:value => #users.tel_no %>
</div>
<div class="edit-address">
<label for="Address">Address</label>
<%= f.text_area :address,:class => 'address_text',:value => #users.address %>
<textarea id="first_name"></textarea>
</div>
<div class="edit-submit">
<%= f.submit 'Update Data',:class => 'btn btn-success' %>
</div>
</div>
<% end %>
</div>
</center>
</div>
</div>
<% end %>
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
#users=User.new
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
def issuebooks
#issues=Issue.new
end
def savedissuebooks
#issues=Issue.new(params[:issues])
if #issues.save
flash[:notice]="information has saved successfully"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:notice]="Data couldnot saved"
flash[:color]="invalid"
render 'issuebooks'
end
end
def availablebooks
#books=Book.all
end
def userissues
#issues=Issue.all
end
def magazine
#magazines=Magazine.new
end
def savemagazines
#users=User.find(params[:id])
#magazines=Magazine.new(params[:magazines])
#magazines.user_id=#users.id
if #magazines.save
flash[:notice]="Data submitted successfully"
flash[:color]="valid"
redirect_to :action => "member"
else
flash[:notice]="Data could not saved"
flash[:color]="invalid"
render 'magazines'
end
end
def magazineissue
#magazines=Magazine.all
#users=User.find #magazines.first.user_id
end
def blog
#blogs=Blog.new
end
def savecomments
#users=User.find(params[:id])
#blogs=Blog.new(params[:blogs])
#blogs.user_id=#users.id
if #blogs.save
flash[:notice]="Comment has been posted successfully"
flash[:color]="valid"
redirect_to :action => "showcomment"
else
flash[:notice]="Comment could not saved"
flash[:color]="invalid"
render 'blog'
end
end
def showcomment
#blogs=Blog.all
end
def newspaper
#newspapers=Newspaper.new
end
def savenewspaper
#users=User.find(params[:id])
#newspapers=Newspaper.new(params[:newspapers])
#newspapers.user_id=#users.id
if #newspapers.save
flash[:notice]="newspaper data saved successfully"
flash[:color]="valid"
redirect_to :action => "member"
else
flash[:alert]="Data could not saved successfully"
flash[:color]="invalid"
render 'newspaper'
end
end
def adminnewspaperissue
#newspapers=Newspaper.all
#users=User.find #newspapers.first.user_id
end
def userprofile
#users=User.find(params[:id])
end
def updatedata
#users=User.find(params[:id])
if #users.update_attributes(params[:users])
flash[:notice]="User Data has updated"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:alert]="Data could not updated"
flash[:color]="invalid"
render :action => 'userprofile'
end
end
end
From the above code the else part is executing inside updatedata action.Please help to resolve this issue.

Define remember method for remember me feature in Rails 3

I want to login with my app and i am getting the following error.
error:
NoMethodError in SessionsController#loginadmin
undefined method `remember' for #<SessionsController:0x267c300>
Rails.root: C:/Site/swargadwar_admin
Application Trace | Framework Trace | Full Trace
app/controllers/sessions_controller.rb:7:in `loginadmin'
My code are as follows.
views/homes/index.html.erb
<div class="container">
<div style="text-align:center;"><img src="/assets/admin.png" style="width:100px; height:120px; " /></div>
<div class="text-div" style="text-align:center;">Swargadwar, Puri Municipality,govt of odisha</div>
<section>
<% if !current_user %>
<div id="container_demo" >
<!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4 -->
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<%= form_for :admin,:url => {:action =>'loginadmin',:controller => 'sessions' } do |f| %>
<h1>Log in</h1>
<p>
<label for="username" class="uname" data-icon="u" > Your email or username </label>
<%= f.email_field :email,placeholder:"mysupermail#mail.com",:id => "username" %>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "password" %>
</p>
<p class="keeplogin">
<%= f.check_box :remember_me,:id => "loginkeeping" %>
<label for="loginkeeping">Keep me logged in</label>
</p>
<p class="login button">
<%= f.submit "Login" %>
</p>
<p class="change_link">
Not a member yet ?
Join us
</p>
<% end %>
</form>
</div>
<div id="register" class="animate form">
<%= form_for :admin,:url => {:action => 'create_registration',:controller => "admins" } do |f| %>
<h1> Sign up </h1>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<%= f.text_field :user_name,placeholder:"mysuperusername690",:id => "usernamesignup" %>
</p>
<p>
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<%= f.email_field :email,placeholder:"mysupermail#mail.com",:id => "emailsignup" %>
</p>
<p>
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
</p>
<p>
<label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
<%= f.password_field :password_confirmation,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
</p>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Add Image</label>
<%= f.file_field :picture %>
</p>
<p class="signin button">
<%= f.submit "Sign Up"%>
</p>
<p class="change_link">
Already a member ?
Go and log in
</p>
<% end %>
</div>
<div class="error-div">
<% if #admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
</section>
</div>
controller/sessions_controller.rb
class SessionsController < ApplicationController
def loginadmin
#admin=Admin.authenticate(params[:admin][:email], params[:admin][:password])
if #admin
session[:user_id]=#admin.id
cookies.signed[:user_id]=#admin.id
params[:admin][:remember_me] == '1' ? remember(#admin) : forget(#admin)
flash[:notice]="Login Successfull"
flash[:color]="valid"
redirect_to :action => "new", :controller => "admins"
else
flash[:notice]="Login Failed"
flash[:color]="invalid"
render 'index'
end
end
def removeuser
session[:user_id] = nil
cookies.delete :user_id
flash[:notice]="user logged out successfully"
flash[:color]="valid"
redirect_to :action => 'index', :controller => 'homes'
end
end
session_helper.rb
module SessionsHelper
def remember(admin)
admin.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end
def forget(admin)
admin.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
end
model/admin.rb
class Admin < ActiveRecord::Base
attr_accessible :email, :password_hash, :password_salt, :picture, :user_name,:password_confirmation,:password, :remember_me
attr_accessor :password
before_save :encrypt_password
mount_uploader :picture, PictureUploader
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 :user_name, :presence => true, :length => {:in => 3..10}
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
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
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
end
As i am new to RoR please help me to solve this error and run this app successfully.
You must include the SessionsHelper in your SessionsController or move the remember and the forget method to the SessionsController.

How to get one model value from another model in ruby on rails?

I got a task to integrate multiple models in a single form.I have one form 'register' and two models buyer and address. But by doing this i can not attach two forms together.
_form.html.erb is
<% #register.buyers.build %>
<%= form_for(#register) do |f| %>
<% if #register.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#register.errors.count, "error") %> prohibited this register from being saved:</h2>
<ul>
<% #register.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<div class="field">
<h4>Buyer</h4>
</div>
<div class="field">
<%# f.fields_for :buyers do |builder| %>
<%= render :partial => "buyer_fields", :locals => {:f => f } %>
<%# end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
_buyer_fields.html.erb is
<% f.fields_for :buyers do |buyers_form| %>
<div class="fields">
<p>
<%= buyers_form.label :name, "Name" %><br/>
<%= buyers_form.text_field :name %>
</p>
<h4>Address</h4>
<% f.fields_for :addresses do |builder| %>
<%= render :partial => 'address_fields', :locals => { :f => builder} %>
<% end %>
</div>
<% end%>
and the _address_fields.html.erb is
<p class="fields">
<table>
<tr>
<td>
<%= f.text_area :name, :rows => "2",:cols => "20" %>
</td>
</tr>
</table>
</p>
register model is
class Register < ActiveRecord::Base
attr_accessible :date, :book_ids,:buyers_attributes
has_many :authorships
has_many :books, :through => :authorships
has_many :buyers
#accepts_nested_attributes_for :buyers, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
accepts_nested_attributes_for :buyers, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
buyer model is
class Buyer < ActiveRecord::Base
belongs_to :register
attr_accessible :addresses_attributes, :name
has_many :addresses, :dependent => :destroy
accepts_nested_attributes_for :addresses, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end
and address model is
attr_accessible :name
belongs_to :buyer
But only register form is displayed. How can i integrate two models in single form in ruby on rails 3.2.9? Please help.
Your nested address view is wrong.
Make some changes in buyer_fields.html.erb
<%= f.fields_for :buyers do |buyers_form| %>
<div class="fields">
<p>
<%= buyers_form.label :name, "Name" %><br/>
<%= buyers_form.text_field :name %>
</p>
<h4>Address</h4>
<%= buyers_form.fields_for :addresses do |builder| %>
<%= render :partial => 'address_fields', :locals => { :f => builder} %>
<% end %>
</div>
<% end %>

Resources