Почему это не работает с Acts_as_taggable_on - PullRequest
0 голосов
/ 16 июля 2011

У меня есть attr_accessor :politics, :tech, :entertainment, :sports, :science, :crime, :business, :social, :nature, :other в моем post.rb (это теги), я хочу, чтобы они были виртуальными.

Тогда в 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 %>

Чтобы каждый из них был установлен как true или false, наконец, в post_controller.rb в def create у меня есть

@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 в консоли, я получаю ответ на все теги # => 'policy, tech, ... nature, other'

Почему нет: business = false, если я не проверю это?

1 Ответ

1 голос
/ 16 июля 2011

Ваш контроллер должен выглядеть следующим образом.

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

Символ сам по себе всегда будет иметь значение true.Это больше похоже на константу, чем на переменную.Поэтому ему никогда не присваивается значение.

...