如何只选择表单中的唯一值?

时间:2018-04-05 01:22:38

标签: ruby-on-rails ruby-on-rails-5

我有一个表单,我只想显示第一个Parameters: {"utf8"=>"✓", "authenticity_token"=>"JZvhwloo0+XM9bmptxXGfnDw==", "reconciliation"=>{"location_id"=>"2", "inventory_item"=> {"quantity_left"=>"1", "quantity_delivered"=>"170"}}, "commit"=>"Update", "location_id"=>"2"} 的唯一值。我需要更改以下表单才能获得该结果?

options _for_select

控制器

<%= form_for @domain, :url => {:controller => "page_scraper", :action => "compare"} do |f| %>
  <%=select_tag 'domain', options_for_select(@savedHTML.collect{ |u| [u.domain, u.domain] })%> 
  <%=select_tag 'version_one', options_for_select(@savedHTML.collect{ |u| [u.created_at, u.created_at] })%>
  <%=select_tag 'version_two', options_for_select(@savedHTML.collect{ |u| [u.created_at, u.created_at] })%>
  <%=f.submit "Compare" %>
<% end %>

4 个答案:

答案 0 :(得分:1)

对集合使用.uniq方法删除重复项。

<%= form_for @domain, :url => {:controller => "page_scraper", :action => "compare"} do |f| %>
  <%=select_tag 'domain', options_for_select(@savedHTML.collect{ |u| [u.domain, u.domain] }.uniq)%> 
  <%=select_tag 'version_one', options_for_select(@savedHTML.collect{ |u| [u.created_at, u.created_at] })%>
  <%=select_tag 'version_two', options_for_select(@savedHTML.collect{ |u| [u.created_at, u.created_at] })%>
  <%=f.submit "Compare" %>
<% end %>

答案 1 :(得分:0)

您可以尝试以下代码。当你在那时对集合执行uniq时,它会给你uniq记录然后你可以对每个记录执行任何操作。

<%= form_for @domain, :url => {:controller => "page_scraper", :action => "compare"} do |f| %>
  <%= select_tag 'domain', options_for_select(@savedHTML.uniq.collect{ |u| [u.domain, u.domain] })%> 
  <%= select_tag 'version_one', options_for_select(@savedHTML.collect{ |u| [u.created_at, u.created_at] })%>
  <%= select_tag 'version_two', options_for_select(@savedHTML.collect{ |u| [u.created_at, u.created_at] })%>
  <%= f.submit "Compare" %>
<% end %>

答案 2 :(得分:0)

在控制器中更新

控制器

def index       
  @savedHTML = ScrapedPage.all.uniq
end

答案 3 :(得分:0)

在Rails 5.1中您可以使用distinct从控制器获取唯一值,然后可以在视图中执行操作

<强>控制器

def index
 @savedHTML = ScrapedPage.all.distinct
end

查看

<%= form_for @domain, :url => {:controller => "page_scraper", :action => "compare"} do |f| %>
  <%=select_tag 'domain', options_for_select(@savedHTML.collect{ |u| [u.domain, u.domain] })%> 
  <%=select_tag 'version_one', options_for_select(@savedHTML.collect{ |u| [u.created_at, u.created_at] })%>
  <%=select_tag 'version_two', options_for_select(@savedHTML.collect{ |u| [u.created_at, u.created_at] })%>
  <%=f.submit "Compare" %>
<% end %>