我正在使用tag-it https://github.com/aehlke/tag-it进行标记。创建标签并保存后,如何在编辑时显示已保存的标签?我尝试过availableTags选项,但无法实现。
- form_for @user_service, :user => current_user, :url => save_service_tag_user_services_url(:user_service_id => params[:user_service_id]) do |f|
%span.error-message
%ul{:id => "stream_add_topic"}
%li#tag-service{:style => "width: 281px !important;"}
#add_tag{:id=>"#{@user_service.id}"}
.url{:style => "float:left;"}
== Add Tag for auto-posting
%ul{:id => "link_tags_#{@user_service.id}", :style => "width:265px", :name => "service_tags[]"}
%li{:id => "stream_topic_text"}
%label{:for => "topicNames"}== Select a topic
%br/
= f.select(:topic_id , Topic.all.collect {|p| [ p.title, p.id ] }, {:prompt => 'Pick a Topic'})
.modalConfirmAct.flRight
.blueBtn
= submit_tag "Save", :class => "smlrBtn"
.yellowBtn
= link_to_close_redbox("#{t :cancel}", :class => "smlrBtn")
:javascript
jQuery(document).ready(function(){
$j("#link_tags_#{@user_service.id}").tagit({ availableTags: ["c++", "java", "php", "javascript", "ruby", "python", "c"] });
任何人都可以帮助我。
答案 0 :(得分:0)
<ul id="your_tagit_block_id">
<% @available_tags.each do |tag| %>
<li><%= tag.title %></li>
<% end %>
</ul>
https://github.com/aehlke/tag-it/blob/master/README.markdown#usage
答案 1 :(得分:0)
在模型中
class UserSerVice < ActiveRecord::Base
has_many :tags
end
class Tag < ActiveRecord::Base
belongs_to :user_service
end
控制器中的
class UserServicesController < ApplicationController
.....
def edit
@service = UserSerVice.find(params[:id])
@tags ||= @service.tags.collect(&:name)
end
end
在视图中根据tagit
<ul id="myTags"></ul>
$("#myTags").tagit({
availableTags: ['<%=j @tags %>']
});