2016-05-10 5 views
-1

Ruby on Railsのボタンをクリックして特定のクエリを実行しようとしています。私はテーブルの履歴を作成しました。最初はすべてのデータを表示しています。ここに私のハムのページですRuby on Railsデータベースクエリを実行中

%h1 History 

%br 
%br 

%table#views 
    %tbody 
    %tr 
     %td 
     %td 
     %input{:required => "", :type => "text"} 
     %td 
     %input{:required => "", :type => "submit", :value => "Search"} 


%table#houses 
    %thead 
    %tr 
     %th UserName 
     %th User ID 
     %th Email 
     %th Rented House 
     %th Start Date 
     %th End Date 
     %th Total Cost ($) 
     %th Rating 
    %tbody 
    - @histories.each do |history| 
     %tr 
     %td= history.user_name 
     %td= history.user_id 
     %td= history.email 
     %td= history.house_address 
     %td= history.start_date 
     %td= history.end_date 
     %td= history.total_cost 
     %td= history.rating 

= link_to 'Back to Welcome page', users_path 

ここにはテキストボックスがあります。検索をクリックするとクエリが実行され、ユーザー名がボックスに入力されたデータが表示されるようにしたいと思います。

誰か助けてくれますか?

は、あなたがそのデータを返すために、単純な検索を行うことができますし、よろしく

+0

私はあなたはウェルカムページで検索しますか? – Kumar

+0

私はそれを検索したい履歴ページを持っています – Akshay

答えて

0

ありがとうございます。この

(モデル)history.rb

def self.search(search) 
    where("title LIKE ?", "%#{search}%") 
end 

など何か*これはSQLLite

(コントローラ)history_controller.rb

def index 
    @histories = Histories.all 
    if params[:search] 
     @histories = Histories.search(params[:search]).order("created_at DESC") 
    else 
     @histories = Histories.all.order('created_at DESC') 
    end 
    end 

(HAML /ビュー)

を使用しています
= form_tag(history_path, :method => "get", id: "search-form") do 
    = text_field_tag :search, params[:search], placeholder: "Search Username", :class => "form-control" 
+0

テキストボックスを表示していて、検索ユーザー名が書かれていますが、何も起こっていません...ユーザー名がテキストボックスに一致するレコードを表示したいのですが。 – Akshay

+0

http://railscasts.com/episodes/37-simple-search-form?autoplay=true –

+0

多分そのビデオがあなたをもっと助けますか? –