-1

我有多個複選框,我需要保存在mysql數據庫表中。 他設法保留除了多重複選框之外的所有表單字段。 我需要序列化保存它們,我該怎麼辦?保存多個複選框4

_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> 

用腳手架創建我的控制器

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 

回答

0

的ActiveRecord :: Base.serializeReference 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"] }