ruby-on-rails - Rails 中的过滤表单?

标签 ruby-on-rails ruby forms filtering

我是 Rails 的新手。我只是搭建了一个小模型。该模型有一个称为类别的字段。现在我想按类别过滤索引页上的条目。

<% form_for @domain do |f| %>
<p>
 Domain:
 <%= f.select(:Domain,%w{ LifeStyle Automobiles FoodAndBeverage Health IT Telecom EntertainmentAndCelebrity Education BankingInvestmentAndInsurance Travel Sports Parenting ConsumerElectronics RealtyAndLogistics CauseLed})%>


 <%= submit_tag "Filter" %>
</p>
<% end %>

<table border="1">
  <tr>
    <th>Domain</th>
    <th>Category</th>
    <th>Course detail</th>
    <th>Nameofblog</th>
    <th>Descriptionofblog</th>
    <th>Smename</th>
    <th>Smecommuntiy</th>
    <th>Smeifnotorkfac</th>
    <th>Noofmemb</th>
    <th>Discussionforumname</th>
    <th>Discussionforumdescription</th>
    <th>Qnasitesname</th>
    <th>Qnasitesnamedesc</th>
    <th>Newssitename</th>
    <th>Newssitedesc</th>
  </tr>

<% @media_universe_entries.each do |media_universe_entry| %>
  <tr>
    <td><%=h media_universe_entry.Domain %></td>
    <td><%=h media_universe_entry.Category %></td>
    <td><%=h media_universe_entry.CourseDetail %></td>
    <td><%=h media_universe_entry.NameOfBlog %></td>
    <td><%=h media_universe_entry.Descriptionofblog %></td>
    <td><%=h media_universe_entry.SMEname %></td>
    <td><%=h media_universe_entry.SMECommuntiy %></td>
    <td><%=h media_universe_entry.SMEIfnotOrkFac %></td>
    <td><%=h media_universe_entry.NoOfMemb %></td>
    <td><%=h media_universe_entry.discussionforumname %></td>
    <td><%=h media_universe_entry.discussionforumdescription %></td>
    <td><%=h media_universe_entry.QNASitesname %></td>
    <td><%=h media_universe_entry.QNASitesnameDesc %></td>
    <td><%=h media_universe_entry.NewsSiteName %></td>
    <td><%=h media_universe_entry.NewsSiteDesc %></td>
    <td><%= link_to 'Show', media_universe_entry %></td>
    <td><%= link_to 'Edit', edit_media_universe_entry_path(media_universe_entry) %></td>
    <td><%= link_to 'Destroy', media_universe_entry, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New media_universe_entry', new_media_universe_entry_path %>

正如你所看到的,我正在尝试创建一个域明智的文件字符串。我该怎么做?

最佳答案

您还应该发布 Controller 方法,以更好地理解模型。

无论如何,假设模型 Something 有一个字符串“类别”列,您不应该对选择的值进行硬编码,除非这些值是固定的,在这种情况下您可能会考虑不使用字符串列。

这是一个快速键入的示例:

# something_controller.rb
  def index
    @categories = Something.find_by_sql("SELECT category FROM somethings GROUP BY category").map &:category
    @somethings = params[:category].blank? ? Something.all : Something.find_all_by_category(params[:category])
  end

这是 View :

<% form_tag(:action => :index) do %>
  <%= select_tag "category", options_for_select(@categories) %>
  <%= submit_tag "Filter" %>
<% end %>

<table>
  <tr>
    <th>foo</th>
    ...
  </tr>
  <% somethings.each do |something| %>
    <tr>
      <td><%= something.foo %></td>
      ...
    </tr>
  <% end %>
</table>

更新:

虽然上面的代码可以工作,但它的编码速度确实太快了,并且建议采取不好的做法。

这里有一个更好的方法:

# app/models/Something.rb
def self.all_categories
  find_by_sql("SELECT category FROM somethings GROUP BY category").map(&:category).select {|x| x}
end

def self.select(category)
  if category
    find_all_by_category(category)
  else
    find :all
  end
end

#app/controllers/something_controller.rb
def index
  @categories = Something.all_categories
  @somethings = Something.select(params[:category])
end

关于ruby-on-rails - Rails 中的过滤表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1345704/

相关文章:

ODT 文件的 HTML 表单上传/*.odt 的 MIME 类型

ruby-on-rails - "Processing by Contoller#method as */*"和 "Processing by BillsController#show as HTML"有什么区别

ruby-on-rails - Rails 3.1.rc4 中的 Omniauth

Ruby:#map 对于 bang 方法通常没有意义,是吗?

c# - ruby 中的 hmac-sha1 不同于 C# HMACSHA1

c# - 在 asp.net 中使用 smtp 和 gmail 提交表单后发送电子邮件

javascript - 如何使用 this.form.submit() 通过 <select> <option> 表单发送值

ruby-on-rails - Rspec 测试搜查总是返回所有实例

ruby-on-rails - 如何在 Rails 4.0 中仅继承 ActiveRecord 模型的连接

ruby - 在没有 DBI 的情况下将 Ruby 连接到 MSSQL 服务器