【问题标题】:Save multiple checkbox rails 4保存多个复选框栏 4
【发布时间】:2015-08-08 18:31:20
【问题描述】:

我有多个复选框需要保存在 mysql 数据库表中。 他设法保留了除多个复选框之外的所有表单字段。 我需要序列化保存它们,我该怎么做?

_form.html.erb

        <div class="form-group">
      <%= f.label :news_source, 'Noticias', :class => 'col-lg-2 control-label' %>
      <div class="col-lg-10">
        <%= f.label :news_source, 'El País' %>
        <%= check_box_tag  "news_source[]", '1' %>
        <br>
        <%= f.label :news_source, 'El Observador' %>
        <%= check_box_tag  "news_source[]", '2' %>
        <br>
        <%= f.label :news_source, 'El Espectador' %>
        <%= check_box_tag  "news_source[]", '3' %>
      </div>
    </div>

使用 Scaffold 创建我的控制器

  def create
raise params.inspect
@emission = Emission.new(emission_params)

respond_to do |format|
  if @emission.save
    format.html { redirect_to @emission, notice: 'La emisión fue creada correctamente.' }
    format.json { render :show, status: :created, location: @emission }
  else
    format.html { render :new }
    format.json { render json: @emission.errors, status: :unprocessable_entity }
  end
 end
end

【问题讨论】:

    标签: mysql ruby-on-rails ruby-on-rails-4 checkbox multiple-checkout


    【解决方案1】:

    ActiveRecord::Base.serialize Reference Link

    例如:

    class User < ActiveRecord::Base
      serialize : news_source_ids
    end
    
    user = Emission.create(:scholarship=> { "name" => "test", "news_source_ids" => ["1", "2"]})
    Emission.find(usernews_source.id).scholarship# => { "name" => "test", "news_source_ids" => ["1", "2"] }
    

    【讨论】:

      猜你喜欢
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 2017-05-05
      • 1970-01-01
      相关资源
      最近更新 更多