0

나는 초보자이며 muptiple 태그를 사용하여 페이지 콘텐츠를 필터링하고 싶습니다. act_as_taggable_on gem을 사용하고 있으며 태그 클라우드를 사용하고 태그에 따라 콘텐츠를 필터링했습니다. 다음 자습서 (http://railscasts.com/episodes/382-tagging)를 사용했습니다. 이제 여러 개의 tag_type을 사용하여 필터링 할 수 없었습니다. , assetType : 여러 태그를 사용하여 페이지 콘텐츠를 필터링하는 방법

는 다음 코드

acts_as_taggable
acts_as_taggable_on article.rb/내 모델에 추가 된 여러 개의 태그를 작성하는 나도 몰라 컨트롤러 productType

. 나는

<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 %> 

을 내 _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> 
이 가

은 누군가가 나를 도울 수있는 index.html.erb 내보기에서 다음과 같은 방법

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

    end 

시도 내 컨트롤러, 색인 및 _form 페이지를 수정해야합니까? 지금은 내 모든 게시물을 보여주는 내가 태그를 클릭하면 내용이 기본 기준점으로이를 사용

답변

1

변경되지 않습니다 :

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 

조정이 시도

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

:

  • blan을 사용하여 null 및 빈 문자열에 대한 각 매개 변수 확인 k 수표. 아마도이 문맥에서 null과 공백이 같을 수도 있습니다.
  • 태그를 배열에 추가하여 모든 태그를 한 번에 전달할 수 있습니다. 호출을 단순화하는 것뿐만 아니라 호출에 매개 변수를 추가하여 일치하는 스타일을보다 명확하게 제어 할 수 있습니다 (예 : 모든 태그 또는 모든 태그와 일치).

호프가 도움이 되길, 행운을 빈다.

+0

대단히 감사합니다 !!!! – lucy

관련 문제