使用Rails 3根据文本字段值显示数据

时间:2015-04-11 10:53:21

标签: ruby ruby-on-rails-3.2

我有一个问题。我想根据文本字段值从数据库中获取数据。这个文本字段将采用两种类型的值。第一个是简单数字(例如-123456789),第二个是这样的(例如-123456789/1)。简单数字存在于一个表的DB中。在" /"之后的第二个数字中。 (即-1)是另一个表的id与第一个表相关联。然后我的目标是当用户将输入" 123456789"数据将根据此数字通过搜索获取​​,何时用户将提供输入" 123456789/1"首先它将拆分数字,并根据两个表中的数字和id(即-123456789和1)获取值。

我在下面解释一下我的一些代码。

舍/ 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 %>

控制器/ 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

这里我已经完成了简单的数字请帮我从第二个输入数字(即-123456789 / 1)从两个表中获取数据。为此,所有操作都在&#34; scan_hcsy&#34;内执行。 method.Atleast帮助我将数字(即-123456789 / 1)拆分为123456789和1,以便我可以根据此数字和ID获取数据。

1 个答案:

答案 0 :(得分:0)

您可以拆分字符串,结果为数组:

"12345678/1".split('/')
=> ["12345678", "1"]

在你的情况下:

splitted = params[:hcsy][:reciept].split('/')
hcys = splitted[0]
table_id = splitted[1]