为 Rails 中的 nil:NilClass 获取未定义的方法“[]” 3
Getting undefined method `[]' for nil:NilClass in Rails 3
我想将文本字段值提取到我的控制器并使用数据库进行搜索,但出现以下错误。
错误:
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'
请检查我下面的代码并尝试解决此错误。
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
请帮助我。
你的参数有问题。
尝试添加一些调试代码,例如:
def scan_hcsy
+ raise ArgumentError if params.nil?
+ raise ArgumentError if params[:hcsy].nil?
+ raise ArgumentError if params[:hcsy][:receipt].nil?
尝试更正此拼写:
- <%= f.text_field :reciept, ...
+ <%= f.text_field :receipt, ...
我想将文本字段值提取到我的控制器并使用数据库进行搜索,但出现以下错误。
错误:
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'
请检查我下面的代码并尝试解决此错误。
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
请帮助我。
你的参数有问题。
尝试添加一些调试代码,例如:
def scan_hcsy
+ raise ArgumentError if params.nil?
+ raise ArgumentError if params[:hcsy].nil?
+ raise ArgumentError if params[:hcsy][:receipt].nil?
尝试更正此拼写:
- <%= f.text_field :reciept, ...
+ <%= f.text_field :receipt, ...