【问题标题】:Why Doesn't this work with Acts_as_taggable_on为什么这不适用于 Acts_as_taggable_on
【发布时间】:2011-07-16 04:30:31
【问题描述】:

我的 post.rb 中有 attr_accessor :politics, :tech, :entertainment, :sports, :science, :crime, :business, :social, :nature, :other(这些是标签),我希望它们是虚拟的。

然后在 new.html.erb 我有

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

<%= f.check_box :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.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 %>

这样每个都设置为真或假,然后,最后,在def create下的post_controller.rb我有

@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

但是,当我在控制台中执行 post.tag_list 时,我得到了所有标签的响应 #=>'politics, tech, ... nature, other'

如果我不检查,为什么不是 :business = false?

【问题讨论】:

    标签: ruby-on-rails filter tags acts-as-taggable-on


    【解决方案1】:

    您的控制器应如下所示。

    @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]
    

    符号本身将始终评估为真。它更像是一个常数而不是一个变量。所以它永远不会被赋值。

    【讨论】:

    • 嗯,这仍然评估为真
    • 你能看看你的日志并发布“参数:”行吗?
    • 参数:{"utf8"=>"✓", "authenticity_token"=>"A9rDVezHQgMqKjspJYp7tSKjObT+hOVt4ycof0Fy6xE=", "post"=>{"title"=>"Title", "content" =>“内容”、“政治”=>“1”、“科技”=>“1”、“娱乐”=>“0”、“体育”=>“0”、“科学”=>“0” , "犯罪"=>"0", "商业"=>"0", "社会"=>"0", "自然"=>"0", "其他"=>"0"}, "承诺" =>“创建帖子”}
    猜你喜欢
    • 1970-01-01
    • 2019-02-10
    • 2013-07-12
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2018-03-01
    • 2023-03-25
    相关资源
    最近更新 更多