从Rails的主页面搜索

时间:2012-11-30 02:29:25

标签: ruby-on-rails search redirect rails-routing

我正在开发我的第一个网络和Rails应用程序,无法弄清楚如何将搜索功能从我的主页面运行到其中一个控制器。

如何发送请求并重定向到结果页面以显示搜索结果。 我不能让这个工作,因为我不知道如何以一种方式路由我的变量@histories将保持结果并显示在显示页面上。

我希望能够从任何页面深入了解搜索并在专用页面上显示结果。

到目前为止,这是我在控制器,模型和部分方面所拥有的。

货件型号:

def self.search(search)
    search_condition = search
    find_by_sql("SELECT cargo_transit_histories.current_location,cargo_transit_histories.updated_at     FROM cargo_transit_histories
INNER JOIN shipments ON shipments.id = cargo_transit_histories.shipment_id WHERE  shipments.tracking_number='search_condition'")
end

跟踪控制器:

def search
  @histories = Shipment.search(params[:search])
  render('show')
end

显示(在跟踪视图中找到):

<div class="search_result">
<%= render 'track/search_results' %>
</div>

_search(部分):

<%= form_tag :controller => 'tracking', :action => 'search', :method => 'get' do %>
  <%= text_field_tag :search, params[:search], :id => 'search_field' %>
  <%= submit_tag "Search", :name => nil %> 
  <%= link_to_function "Clear", "$('search_field').clear()" %>
<% end %> 

_search_results(partial):

<div class="Results list">

  <table class="Resultslisting" summary="Result list">
  <tr class="header">
  <th>Current Location</th>
  <th>Date/Time</th>
  </tr>
<% if !@histories.empty? %>
  <% @histories.each do |result| %>
  <tr>
  <td><%= result.current_location %></td>
  <td><%= result.updated_at %></td>
  </tr>
  <% end %>
  </table>
<% else %>
   <p> The tracking number does not exist!</p>
<% end %>

</div>

1 个答案:

答案 0 :(得分:0)

尝试类似下面的改编示例:

https://gist.github.com/4173716

但是你需要深入研究Rails 3才能理解为什么。