开发者

Why Doesn't this work with Acts_as_taggable_on

I have attr_accessor :politics, :tech, :entertainment, :sports, :science, :crime, :business, :social, :nature, :other in my post.rb (these are the tags), I want them to be virtual.

Then in the new.html.erb i have

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

<%= f.check_box :tech %>
<%= f.label :tech, 'Technology' %&开发者_开发知识库gt;

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

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

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

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

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

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

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

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

So that each are set as either true or false, then, lastly, in the post_controller.rb under def create i have

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

However when I do post.tag_list in the console I get the response of all of the tags #=>'politics, tech, ... nature, other'

Why isn't :business = false if I dont check it?


Your controller should look like this.

@post.tag_list.clear
@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]

A symbol by itself will always evaluate to true. It's more like a constant than a variable. So it never has a value assigned to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜