[英]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, :productTypeactions_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: 调整项:
Hope that helps, good luck! 希望有帮助,祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.