2010-10-19 161 views
0

我对rails非常陌生,希望这应该是一个快速修复。我正在编写一个应用程序,用于搜索数据库并重新载入显示所需结果的页面。在rails中,如何将输入保存到text_field中并将其发布,以便可以在查询中检索和使用它以检索数据。Ruby on Rails,发布变量

我的观点:

<title></title> 
</head> 

<body> 

    Search Collection <br> 
    <%= text_field "person", "name" %> 
    <%= select_tag(:search_id, '<option value="0">Search by</option><option  value="1">Make</option><option value="2">Condition</option> 
           <option value="3">Sport</option>') %> 
     <%= link_to 'New catalog', new_catalog_path %> 
    <br> 
    <br> 
    <%= link_to "Search", :search_text => , :action => :index %> <br> 
    <br> 


    <h1>Results</h1> 
    <%= will_paginate @catalogs %> 

    <table border="1"> 
     <tr> 
      <th>Catalog id</th> 

      <th>Year</th> 

      <th>Make</th> 

      <th>Card number</th> 

      <th>Number</th> 

      <th>Condition</th> 

      <th>Sport</th> 

      <th>Tracking list</th> 
     </tr> 
     <% for catalog in @catalogs %> 

     <tr> 
      <td><%= catalog.Catalog_ID %></td> 

      <td><%= catalog.Year %></td> 

      <td><%= catalog.Make %></td> 

      <td><%= catalog.Card_Number %></td> 

      <td><%= catalog.Number %></td> 

      <td><%= catalog.Condition %></td> 

      <td><%= catalog.Sport %></td> 

      <td><%= catalog.Tracking_List %></td> 

      <td><%= link_to 'Show', catalog %></td> 

      <td> 
      <%= link_to 'Edit', edit_catalog_path(catalog) %></td> 

      <td> 
      <%= link_to 'Destroy', catalog, :confirm => 'Are you sure?', :method => :delete %></td> 
     </tr> 
     <% end %> 
    </table> 
    <br> 

</body> 

我的控制器方法

def index 
@search_text = 'T206 White Border' 

@catalogs = Catalog.paginate_by_sql ['select * from catalogs where Make =\''+ @search_text+'\'' , 80000], :page => params[:page] 


end 

温柔如果一个简单的办法,我还是习惯到整个MVC东西

回答

1

你的问题有一个很多继续,所以让我们尝试一次一个整理。首先,我假设你的数据库有一个名为catalogs的表,其中有一个名为make的列,并且你正在使用will_paginate插件。看起来您已经开始直接从文档复制和修改一些示例。首先,你的控制器 - 你不需要更复杂的paginate_by_sql,你可以使用更简单的paginate

控制器:

def index 
    @catalogs = Catalog.paginate(:all, :conditions => {:make => params[:search]}, :page => params[:page]) 
end 

现在您的看法,只是相关的搜索的东西:

<% form_tag catalogs_path, :method => :get do %> 
    <%= text_field_tag 'search' %> 
    <%= submit_tag 'submit search' %> 
<% end %> 

就是这样。祝你好运!

+0

这已经解决了很多问题哈哈感谢一吨。你是明智的 – 2010-10-20 05:29:54

+0

嗨,它看起来像你是新的stackoverflow。通过接受我的回答,我得到了帮助你的信贷。如果你不接受来自帮助你的人的好答案,你最终会停止寻求帮助。每次提问时,都会显示您的接受百分比,以便人们知道您是否接受答案。 – 2010-10-23 02:12:31