Can anybody please help me to resolve this error.When i am updating my db values i am getting the below error.
Error: undefined method `update_attributes' for nil:NilClass
My code snippets 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_show_path%></p>
</div>
</center>
views/users/show.html.erb
<h1>Show your data</h1>
<center>
<ul>
<% @user.each do |t| %>
<li>
<%= t.name %> |
<%= t.email %> |
<%= t.password%> |
<%= t.created_at %>
<%= link_to "edit",users_edit_path(:id => t.id) %>
</li>
<% end %>
</ul>
<div class="back_btn">
<a href="/users/index"><button type="button" class="btn-custom " style="cursor:pointer;">Back</button></a>
</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>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field :password_confirmation %>
</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 %>
views/users/edit.html.erb:
<h1>Edit your data here</h1>
<center>
<%= form_for @user ,:url => {:action => "update"} 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>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field :password_confirmation %>
</p>
<center>
<%= f.submit "Update",: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
def show
@user=User.all
end
def edit
@user=User.new
end
def update
@user=User.find_by_id(params[:id])
if @user.update_attributes(update_params)
flash[:notice]="Your data is updated succesfully"
flash[:color]="valid"
redirect_to :action => 'show'
else
flash[:alert]="Your data could not update,Please check it..!!"
flash[:color]="invalid"
redirect_to :action => 'edit'
end
end
private
def users_param
params.require(:user).permit(:name, :email, :password,:password_confirmation)
end
def update_params
params.require(:user).permit(:name,:email,:password,:password_confirmation)
end
end
config/routes.rb:
Rails.application.routes.draw do
root "users#index"
get "users/new" => "users#new"
get "users/index" => "users#index"
post "users/create" => "users#create"
get "users/show" => "users#show"
get "users/edit" => "users#edit"
post "users/update" => "users#update"
end
model/user.rb:
class User < ActiveRecord::Base
#attr_accessor :password
#attr_accessor :name
#attr_accessor :email
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
Please help me to resolve the given error.I am using rails version-4 and ruby 1.9.3.
Aucun commentaire:
Enregistrer un commentaire