如何使帖子被复选框“标记”?

时间:2011-08-30 18:04:55

标签: ruby-on-rails ruby-on-rails-3 checkbox form-for

我正在使用act-as-taggable-on。我尝试了多种方法,但都没有。

在视图上:

    <%= check_box("post", "add_politics", {:class=> "post"}) %>
<%= f.label :politics %>

<%= check_box("post", "add_tech") %>
<%= f.label :tech, 'Technology' %>

<%= f.check_box :entertainment %>
<%= f.label :entertainment %>

<%= f.check_box :sports %>
<%= f.label :sports %>

<%= f.check_box :science %>
<%= f.label :science %>
    <%= f.submit %>

在模型中:

  def add_politics; self.tag_list.add('politics'); end
  def add_tech; self.tag_list << 'tech'; end
  def add_entertainment;  self.tag_list << 'entertainment'; end
  def add_sports; self.tag_list << 'sports'; end
  def add_science; self.tag_list << 'science'; end
  def add_crime;  self.tag_list << 'crime'; end
  def add_business; self.tag_list << 'business'; end
  def add_social; self.tag_list << 'social'; end
  def add_nature; self.tag_list << 'nature'; end
  def add_other; self.tag_list << 'other'; end

在控制器中:

 @post.tag_list << 'politics' if params[:post][:politics]
 @post.tag_list << 'tech' if params[:post][:tech]
 @post.tag_list << 'entertainment' if params[:post][:entertainment]
 @post.tag_list << 'sports' if params[:post][:sports]
 @post.tag_list << 'science' if params[:post][:science]
 @post.tag_list << 'crime' if params[:post][:crime]
 @post.tag_list << 'business' if params[:post][:business]
 @post.tag_list << 'social' if params[:post][:social]
 @post.tag_list << 'nature' if params[:post][:nature]
 @post.tag_list << 'other' if params[:post][:other]

最终发生的事情是只有娱乐,体育,科学......其他人才会被展示,因为那些是&lt;%= f.check_box:tag%&gt;格式。但取消选中或检查它们并没有什么区别 - 这些类型的标签将始终显示。到底是怎么回事?

1 个答案:

答案 0 :(得分:0)

我修好了。方法如下:

<%= check_box_tag 'tags[]', 'politics' %>
<%= f.label :politics %>

    <%= check_box_tag 'tags[]', 'tech' %>
<%= f.label :tech, 'Technology' %>

<%= check_box_tag 'tags[]', 'entertainment' %>
<%= f.label :entertainment %>

<%= check_box_tag 'tags[]', 'sports' %>
<%= f.label :sports %>

<%= check_box_tag 'tags[]', 'science' %>
<%= f.label :science %>

<%= check_box_tag 'tags[]', 'crime' %>
<%= f.label :crime %>

<%= check_box_tag 'tags[]', 'business' %>
<%= f.label :business %>

<%= check_box_tag 'tags[]', 'social' %>
<%= f.label :social %>

<%= check_box_tag 'tags[]', 'nature' %>
<%= f.label :nature %>

<%= check_box_tag 'tags[]', 'other' %>
<%= f.label :other %>

在控制器中:

  @post.tag_list = params[:tags]
相关问题