使用 Rails 根据文本字段值显示数据 3
Display data according to text field value using Rails 3
我有一个 issue.I 想根据文本字段从数据库中获取数据 value.This 文本字段需要两种类型 value.First 一种是简单的数字(例如 123456789)第二个是这样的(e.g-123456789/1)。数据库中的简单数字是一个 table.In “/”(即 1)之后的第二个数字是另一个 table与第一个关联的 ID table.Then 我的目标是当用户输入“123456789”时,数据将通过搜索根据此数字获取,当用户首先输入“123456789/1”时,它将拆分数字和值将根据数字和 id(即 123456789 和 1)从 table.
中获取
这里我在下面解释我的一些代码。
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",:onchange => 'this.form.submit();' %>
<% end %>
<% if params[:id] %>
<center><h1>HARICHANDRA SAHAYATA YOJANA SLIP</h1></center>
Receipt No :<%= @hcsys.Receipt_No %>
<div class="left-content">
<p>Deceased Name :</p> <%= @hcsys.Deceased_Name %>
<p>Beneficary name :</p> <%= @hcsys.Beneficiary_Name %>
<p>Relation with Deceased :</p> <%= @hcsys.Beneficiary_Rel_With_Decease %>
<p>Address :</p> <%= @hcsys.Address %>
<p>Police station :</p> <%= @hcsys.PoliceStation %>
<p>Mobile No :</p> <%= @hcsys.Mobile_No %>
<p>Occupation :</p> <%= @hcsys.Occupation %>
<p>Brahmin :</p> <%= @hcsys.Brahmin %>
<p>Amount Required :</p> <%= @hcsys.Amount_Required %>
<p>Has He/She recieved any assistance erlier from this fund :</p> <%= @hcsys.Recieved_Fund_Earlier %>
</div>
<div class="right-content">
<p>BPL :</p> <%= @hcsys.BPL %>
<p>Govt. Service :</p> <%= @hcsys.Govt_Service %>
<p>Business :</p> <%= @hcsys.Business %>
<p>Land of property :</p> <%= @hcsys.Land_Property %>
<p>Other :</p> <%= @hcsys.Others %>
</div>
<% 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 => "hcsy_details",:id1 => params[:id],:id2 => @hcsy.id
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][:reciept])
if @hcsy
flash[:notice]="Check the record"
flash[:color]="valid"
redirect_to :action => 'hcsy',:id => @hcsy.id
else
flash[:alert]="Receipt number could not found"
flash[:color]="invalid"
render 'hcsy'
end
end
def hcsy
if params[:id]
@hcsys=THcsy.find(params[:id])
end
end
def scanrecord
@hcsy=THcsy.find(params[:id])
end
def hcsy_deatils
@t_hcsy=THcsyFundTypeMaster.new
end
def create_details
@t_hcsy=THcsyFundTypeMaster.new(params[:t_hcsy])
if @t_hcsy.save
flash[:notice]="Check the record"
flash[:color]="valid"
redirect_to :action => 'hcsy_details_master',:id1 => params[:id1] ,:id2 => params[:id2] , :id3 => @t_hcsy.HCSY_Fund_Type_ID
else
flash[:alert]="Receipt number could not found"
flash[:color]="invalid"
render 'hcsy_deatils'
end
end
def hcsy_details_master
@t_hcsy_master=THcsyDetails.new
end
def create_details1
@admin=Admin.find(params[:id1])
@hcsy=THcsy.find(params[:id2])
@t_hcsy=THcsyFundTypeMaster.find_by_HCSY_Fund_Type_ID(params[:id3])
@t_hcsy_master=THcsyDetails.new(params[:t_hcsy_master])
@t_hcsy_master.Created_By=@admin.id
@t_hcsy_master.HCSY_ID=@hcsy.id
@t_hcsy_master.HCSY_Fund_Type_ID=@t_hcsy.HCSY_Fund_Type_ID
if @t_hcsy_master.save
flash[:notice]="Record has created"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="Record could not create"
flash[:color]="invalid"
render 'hcsy_details_master'
end
end
end
我在这里完成了简单的数字,请帮助我通过第二个输入数字(i.e-123456789/1)从两个 table 中获取数据。为此,所有操作都在 [=23] 中执行=] method.Atleast 帮我把number(i.e-123456789/1) 拆分为123456789 和1 这样我就可以根据这个number 和id 取数据了。
您可以将字符串拆分为数组:
"12345678/1".split('/')
=> ["12345678", "1"]
你的情况:
splitted = params[:hcsy][:reciept].split('/')
hcys = splitted[0]
table_id = splitted[1]
我有一个 issue.I 想根据文本字段从数据库中获取数据 value.This 文本字段需要两种类型 value.First 一种是简单的数字(例如 123456789)第二个是这样的(e.g-123456789/1)。数据库中的简单数字是一个 table.In “/”(即 1)之后的第二个数字是另一个 table与第一个关联的 ID table.Then 我的目标是当用户输入“123456789”时,数据将通过搜索根据此数字获取,当用户首先输入“123456789/1”时,它将拆分数字和值将根据数字和 id(即 123456789 和 1)从 table.
中获取这里我在下面解释我的一些代码。
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",:onchange => 'this.form.submit();' %>
<% end %>
<% if params[:id] %>
<center><h1>HARICHANDRA SAHAYATA YOJANA SLIP</h1></center>
Receipt No :<%= @hcsys.Receipt_No %>
<div class="left-content">
<p>Deceased Name :</p> <%= @hcsys.Deceased_Name %>
<p>Beneficary name :</p> <%= @hcsys.Beneficiary_Name %>
<p>Relation with Deceased :</p> <%= @hcsys.Beneficiary_Rel_With_Decease %>
<p>Address :</p> <%= @hcsys.Address %>
<p>Police station :</p> <%= @hcsys.PoliceStation %>
<p>Mobile No :</p> <%= @hcsys.Mobile_No %>
<p>Occupation :</p> <%= @hcsys.Occupation %>
<p>Brahmin :</p> <%= @hcsys.Brahmin %>
<p>Amount Required :</p> <%= @hcsys.Amount_Required %>
<p>Has He/She recieved any assistance erlier from this fund :</p> <%= @hcsys.Recieved_Fund_Earlier %>
</div>
<div class="right-content">
<p>BPL :</p> <%= @hcsys.BPL %>
<p>Govt. Service :</p> <%= @hcsys.Govt_Service %>
<p>Business :</p> <%= @hcsys.Business %>
<p>Land of property :</p> <%= @hcsys.Land_Property %>
<p>Other :</p> <%= @hcsys.Others %>
</div>
<% 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 => "hcsy_details",:id1 => params[:id],:id2 => @hcsy.id
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][:reciept])
if @hcsy
flash[:notice]="Check the record"
flash[:color]="valid"
redirect_to :action => 'hcsy',:id => @hcsy.id
else
flash[:alert]="Receipt number could not found"
flash[:color]="invalid"
render 'hcsy'
end
end
def hcsy
if params[:id]
@hcsys=THcsy.find(params[:id])
end
end
def scanrecord
@hcsy=THcsy.find(params[:id])
end
def hcsy_deatils
@t_hcsy=THcsyFundTypeMaster.new
end
def create_details
@t_hcsy=THcsyFundTypeMaster.new(params[:t_hcsy])
if @t_hcsy.save
flash[:notice]="Check the record"
flash[:color]="valid"
redirect_to :action => 'hcsy_details_master',:id1 => params[:id1] ,:id2 => params[:id2] , :id3 => @t_hcsy.HCSY_Fund_Type_ID
else
flash[:alert]="Receipt number could not found"
flash[:color]="invalid"
render 'hcsy_deatils'
end
end
def hcsy_details_master
@t_hcsy_master=THcsyDetails.new
end
def create_details1
@admin=Admin.find(params[:id1])
@hcsy=THcsy.find(params[:id2])
@t_hcsy=THcsyFundTypeMaster.find_by_HCSY_Fund_Type_ID(params[:id3])
@t_hcsy_master=THcsyDetails.new(params[:t_hcsy_master])
@t_hcsy_master.Created_By=@admin.id
@t_hcsy_master.HCSY_ID=@hcsy.id
@t_hcsy_master.HCSY_Fund_Type_ID=@t_hcsy.HCSY_Fund_Type_ID
if @t_hcsy_master.save
flash[:notice]="Record has created"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="Record could not create"
flash[:color]="invalid"
render 'hcsy_details_master'
end
end
end
我在这里完成了简单的数字,请帮助我通过第二个输入数字(i.e-123456789/1)从两个 table 中获取数据。为此,所有操作都在 [=23] 中执行=] method.Atleast 帮我把number(i.e-123456789/1) 拆分为123456789 和1 这样我就可以根据这个number 和id 取数据了。
您可以将字符串拆分为数组:
"12345678/1".split('/')
=> ["12345678", "1"]
你的情况:
splitted = params[:hcsy][:reciept].split('/')
hcys = splitted[0]
table_id = splitted[1]