vendredi 10 avril 2015

Getting undefined method `[]' for nil:NilClass in Rails 3

I want to fetch text field value to my controller and do the search with database but i got the following error.


Error:



NoMethodError in HomesController#scan_hcsy

undefined method `[]' for nil:NilClass
Rails.root: C:/Site/swargadwar

Application Trace | Framework Trace | Full Trace
app/controllers/homes_controller.rb:38:in `scan_hcsy'


Please check my below code and try to resolve this error.


homes/hcsy_html.erb



<% if current_admin %>
<div class="header">
<div class="navbar-header">Swargadwar, Puri Municipality,govt of odisha</div>
<div class="nav navbar-top-links navbar-right">
<div class="image"></div>
</div>
<div class="name-div">

</div>
</div>
<div class="menu-div">
<div id="leftsidebtn">
<ul>
<li><a href="/homes/registration">Create User</a></li>
<li><a href="/homes/hcsy">Scan Report</a></li>
<li><a href="#">View and Payment Report</a>
<ul>
<li><a href="/homes/hcsy">HCSY</a></li>

</ul>
</li>

<li><a href="#">Payment Validate</a></li>
<li><a href="/sessions/removeadmin">Log Out</a></li>
</ul>
</div>
</div>
<div class="content-div">
Logged in as:<%= current_admin.email %>
<center><h1>HARICHANDRA SAHAYATA YOJANA SLIP</h1></center>
<%= form_for :hcsy,:url => {:action =>'scan_hcsy' } do |f| %>
<%= f.text_field :reciept,placeholder:"Get your scan code" %>
<%= f.submit "search" %>
<% end %>
</div>

<% end %>


controller/homes_controller.rb



class HomesController < ApplicationController
def index

end
def registration
@user=User.new
end
def usersave
@admin=Admin.find(params[:id])
@user=User.new(params[:user])
@user.admin_id=@admin.id
if @user.save
flash[:notice]="User has created successfully"
flash[:color]="valid"
redirect_to :action => "index"
else
flash[:alert]="User could not created"
flash[:color]="invalid"
render 'registration'
end
end
def hcsy_reg
@hcsy=THcsy.new
end
def create_reg
@hcsy=THcsy.new(params[:hcsy])
if @hcsy.save
flash[:notice]="Data has saved successfully"
flash[:color]="valid"
redirect_to :action => "index"
else
flash[:alert]="Data could not saved successfully"
flash[:color]="invalid"
render 'hcsy_reg'
end
end
def scan_hcsy
@hcsy=THcsy.find_by_Receipt_No(params[:hcsy][:receipt])
if @hcsy
flash[:notice]="Check the record"
flash[:color]="valid"
redirect_to :action => 'scanrecord'
else
flash[:alert]="Receipt number could not found"
flash[:color]="invalid"
render 'hcsy'
end
end
def hcsy
@hcsy=THcsy.new
end
def scan_record

end
end


model/t_hcsy.rb



class THcsy < ActiveRecord::Base
attr_accessible :Address, :Amount_Required, :B_Audio, :B_Thumb, :B_photo, :Beneficiary_Name, :Beneficiary_Rel_With_Decease, :Brahmin, :Business, :Created_by, :D_photo, :Date_Of_Required, :Deceased_Name, :Govt_Service, :HCSY_ID, :Land_Property, :Mobile_No, :Occupation, :Others, :PoliceStation, :Prev_Amount_Received, :Prev_Date_Recieved, :Prev_Receipt_No, :Receipt_No, :Recieved_Fund_Earlier, :Sdp_Id, :Updated_By,:BPL
attr_accessor :receipt
end


Please help me.


Aucun commentaire:

Enregistrer un commentaire