简体   繁体   English

如何使用多个标签过滤页面内容

[英]how can I filter page content using multiple tags

I am newbie in rails and I want to filter my page content using muptiple tags. 我是Rails的新手,我想使用多个标签过滤页面内容。 I am using act_as_taggable_on gem and I managed to have a tag-cloud and filter my content according tags. 我正在使用act_as_taggable_on gem,并设法创建了一个标签云,并根据标签过滤了我的内容。 I used the following tutorial ( http://railscasts.com/episodes/382-tagging ). 我使用了以下教程( http://railscasts.com/episodes/382-tagging )。 Now I couldn't manage to filter using multiple tag_types. 现在,我无法使用多个tag_types进行过滤。

I added in my model/ article.rb the following code 我在模型/ article.rb中添加了以下代码

acts_as_taggable actions_as_taggable
acts_as_taggable_on :assetType, :productType actions_as_taggable_on:assetType,:productType

in controller I don't know to write multiple tags . 在控制器中,我不知道写多个标签。 I tried the following way 我尝试了以下方法

def index
  if (params[:assetType] and params[:productType])
   @articles = Article.tagged_with(params[:assetType]).tagged_with(params[:productType])
  else
      @articles = Article.all
    end

  end

In my view in index.html.erb I have 在index.html.erb中,我有

<div id="tag_cloud">
  <% tag_cloud Article.productType_counts, %w[s m l] do |tag, css_class| %>
    <%= link_to tag.name, tag_path(tag.name), class: css_class %>
  <% end %>
</div>
<div id="tag_cloud_asset">
  <% tag_cloud Article.assetType_counts, %w[s m l] do |tag, css_class| %>
    <%= link_to tag.name, tag_path(tag.name), class: css_class %>
  <% end %>
</div>
<div class="article-content">  
  <% @articles.each do |article| %>    
      <h3><%= article.title %></h3>
      <p><%= article.content %></p>  

  <% end %>

and in my _form I have 在我的_form中

<%= form_for(@article) do |f| %>
    <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.label :assetType_list, "Tags (Asset Type separated by commas)" %><br />
    <%= f.text_field :assetType_list %>
    <%= f.label :productType_list, "Tags (Product Type separated by commas)" %><br />
    <%= f.text_field :productType_list %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

Could someone help me how should I modilfy my controller, index and _form page? 有人可以帮助我如何修改控制器,索引和_form页面吗? now it is showing all my posts and when I click on tags the content is not changed 现在它显示了我所有的帖子,当我单击标签时,内容没有更改

Using this as a basic reference point: 以此作为基本参考点:

https://github.com/mbleigh/acts-as-taggable-on#finding-tagged-objects https://github.com/mbleigh/acts-as-taggable-on#finding-tagged-objects

Try this: 尝试这个:

def index
  tags = []
  tags << params[:assetType] unless params[:assetType].blank?
  tags << params[:productType] unless params[:productType].blank?

  if tags.count == 2
    @articles = Article.tagged_with(tags)
  else
    @articles = Article.all
  end
end

Adjustments: 调整项:

  • Checking each parameter for null and empty string using blank check. 使用空白检查检查每个参数的空字符串和空字符串。 Perhaps it's possible that null and blank are the same in this context. 在这种情况下,null和blank可能相同。
  • Adding tags to an array, so that I can pass them all at once. 将标签添加到数组中,以便我可以一次全部传递它们。 Not just to simplify the call, but you can have more explicit control over the matching style by adding additional parameters to the call (such as match all or any tags). 不仅可以简化调用,还可以通过向调用中添加其他参数(例如匹配所有或任何标签)来对匹配样式进行更明确的控制。

Hope that helps, good luck! 希望有帮助,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何消除使用操作文本(trix 编辑器)编写的视图页面中随内容生成的 html 标签? - how can I eliminate the html tags generated along with the content in the view page, written using the Action text(trix Editor)? 如何使用safe_concat创建多个content_tags? - How to create multiple content_tags using safe_concat ? 如何在Rails ActiveRecord中独家过滤相关模型中的标签 - How can I filter tags in a related model exclusively in Rails ActiveRecord 我怎样才能多个2个text_field_tags? - How can i multiple 2 text_field_tags? 如何使用 ActiveRecord 按多个类别过滤产品? - How can I filter products by multiple categories with ActiveRecord? 如何使用Rails 3中的控制器中的include过滤视图中的结果? - How can i filter results in the view using includes in the controller in rails 3? 我怎样才能获得current_page? 匹配多个动作? - How can I get current_page? to match multiple actions? 如何在页面上多次使用相同的布局? - How can I use the same layout multiple times on a page? 如何通过多个值对索引页面上的记录进行排名? - How can I rank records on the index page by multiple values? 使用meta标签gem,如何在content_for中呈现metas? - Using meta tags gem, how to render metas in content_for?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM