0

가 :다형성 협회 폼 tag_list

<div class="field"> 
<%= form_for [@tagable, Tag.new] do |f| %> 
    <p> 
     <%= f.label :Tag %><br /> 
     <%= f.text_field :tag_list %> 
    <p> 
    <div class="actions"> 
     <%= f.submit "Create Product", class: "btn btn-large btn-primary" %> 

    </div> 
<% end %> 
</div> 

내 태그 모델에서 방법을 정의했습니다 :

def tag_list 
tags.map(&:name).join(", ") 
end 

def tag_list=(tags_string) 

    tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq 
    logger.debug "#{tag_names}\n\n\n\n\n\n" 

    tag_names.each do |tag_name| 
     #tag = Tag.find_or_initialize_by_name(tag_name) 
     #tag.save 
     tag = Tag.find_or_create_by_name(tag_name) 

     logger.debug "#{tag}\n\n\n\n\n\n" 

    end 
end 

에서 코드 줄 "find_or_create ...."아무튼 ' 콘솔에서 작동하는 것처럼 보이지만 마침내 코드는 name 특성을 사용하지 않습니다. 이처럼 : 나는 콘솔에 인쇄했습니다 그 코드 전에

INSERT INTO "tags" ("created_at", "name", "tagable_id", "tagable_type", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Fri, 19 Oct 2012 01:04:31 UTC +00:00], ["name", nil], ["tagable_id", 19], ["tagable_type", "Product"], ["updated_at", Fri, 19 Oct 2012 01:04:31 UTC +00:00]] 

좋아 I'would은 "태그"의 내용을 저장합니다 :

Tag Load (0.1ms) SELECT "tags".* FROM "tags" WHERE "tags"."name" = 'musica' LIMIT 1 
    (0.0ms) begin transaction 
    (0.1ms) commit transaction 

을 나는 각각의 태그를 만들 수있는 방법 "tag_list"? PS : 나는 또한 "태그 모델"내 이해 당으로

+0

이 태그 기능에 플러그인을 사용하고 있습니까? – Samiron

답변

0

에서 주석 코드를 시도했습니다, 당신은 어떤 taggable에 태그를 추가 할 text field을 보이고있다. 이 텍스트 필드에는 태그 목록 (tag_list)이 포함될 수 있습니다. 양식을 제출하면 taggable이 해당 태그와 연결됩니다.

이 경우 acts_as_taggable plugin을 시도해 볼 수 있습니다 (기본값 : tag_list). 다양한 옵션이 있습니다.

일부 참조

은 필자가 귀하의 요구 사항 중 하나를 놓친 경우 알려주세요 링크.

+0

흠, 문제는 내가 자기 자신의 태깅 시스템을 만들고 있는데, 다형성 협회를 사용하고 있기 때문에, acts_as_taggable 젬을 사용하고 싶지 않습니다. 여러 튜토리얼을 살펴 보았습니다. 예를 들어, 제품 : product 모델과 태그 모델, 그래서 그들은 제품의 모델에서 다음과 같이 할 수 있습니다 : def tag_list self.product.tags = collect .... etc,하지만 저는 태그 모델에서 그런 식으로하고 싶습니다. 내 태그 모델 내에 tag_list 메소드를 정의하십시오! –