I'm using acts-as-taggable-on. I've tried multiple approaches but none of them work.
On the view:
<%= check_box("post", "add_politics", {:class=> "post"}) %>
<%= f.label :politics %>
<%= c开发者_如何学JAVAheck_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 %>
In the model:
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
In the controller:
@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]
What ends up happening is that only entertainment, sports, science...other get displayed because those are the ones that have <%=f.check_box :tag%> format. But unchecking or checking them doesn't make a difference -- those types of tags will always appear. What the heck is going on?
I fixed it. Here's how:
<%= 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 %>
In the controller:
@post.tag_list = params[:tags]
精彩评论