Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使帖子通过复选框“标记”?_Ruby On Rails_Ruby On Rails 3_Checkbox_Form For - Fatal编程技术网

Ruby on rails 如何使帖子通过复选框“标记”?

Ruby on rails 如何使帖子通过复选框“标记”?,ruby-on-rails,ruby-on-rails-3,checkbox,form-for,Ruby On Rails,Ruby On Rails 3,Checkbox,Form For,我用acts作为标记。我尝试过多种方法,但都不管用 关于这一观点: <%= check_box("post", "add_politics", {:class=> "post"}) %> <%= f.label :politics %> <%= check_box("post", "add_tech") %> <%= f.label :tech, 'Technology' %> <%= f.check_box :entert

我用acts作为标记。我尝试过多种方法,但都不管用

关于这一观点:

    <%= 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]
  @post.tag_list = params[:tags]

最终发生的事情是,只有娱乐、体育、科学……其他的东西会被展示出来,因为这些都是有格式的。但是,取消选中或检查它们并没有什么区别——这些类型的标记总是会出现。到底发生了什么事?

我把它修好了。以下是方法:

<%= 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 %>

视图可以使用一些重构。它可以被改写成3行,你的手不会因为一遍又一遍地输入相同的代码而受伤吗?试着重构一下