Ruby on Rails,发布变量

时间:2010-10-19 03:19:11

标签: ruby-on-rails

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

我的观点:                        

    <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 个答案:

答案 0 :(得分: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 %>

就是这样。祝你好运!