ruby - Rails 4: Save Checkbox Results to Serialized Array -


i have campaign model channel column. channel store serialized array of chosen results via checkboxes.

here's model..

app/models/campaign.rb

class campaign < activerecord::base     serialize :channels, array end 

app/controllers/compaigns_controller.rb

class campaignscontroller < applicationcontroller       def index         @campaigns = campaign.all.order("created_at desc")       end        def new         @campaign = campaign.new       end        def create         @campaign = campaign.new(campaign_params)           if @campaign.save              zip = uploadzip.find(params[:uploadzip_id])             zip.campaign = @campaign             zip.save              flash[:success] = "campaign launched!"             redirect_to @campaign         else             flash[:error] = "there problem launching campaign."             redirect_to new_campaign_path         end       end        def show         @campaign = campaign.includes(:program, :uploadzip, :channel, :plan, :uploadpdfs).find(params[:id])       end    private        def campaign_params         params.require(:campaign).permit(:name, :comment, :channel, :plan_id, :program_id, :campaign_id, uploadpdf_ids: [])       end end 

the part of form checkboxes..

views/campaigns/_target.rb

<%= form_for @campaign, url: {action: "create"} |f| %>     ...  <label class="right-inline">    <%= f.label :channel, "distribution channel", class: "right-label" %> </label>  <ul class="channel-list">     <% ["folder", "fax", "email"].each |channel| %>      <li><%= check_box_tag :channel, channel %> <%= channel %>         <% end %></li> </ul>  ...  <% end %> 

i'm having problems saving these results inside campaign object.

any highly appreciated.

first of all, mentioned column name channel, have used plural version in campaign model. since planning save array of channels in column, suggest change name of column in database channels. code below assumes change database column channels.

since serializing channels attribute array , form send parameter array controller, need update campaign_params method accordingly.

def campaign_params   params.require(:campaign).permit(:name, :comment, :plan_id, :program_id, :campaign_id, uploadpdf_ids: [], channels: []) end 

now, relevant part of @campaign form should this:

<ul class="channels-list">   <% ["folder", "fax", "email"].each |channel| %>     <li><%= check_box_tag "campaign[channels][]", channel, @campaign.channels.include?(channel), id: "campaign_channels_#{channel}" %> <%= channel %></li>   <% end %> </ul> 

explanation

first argument in check_box_tag name attribute of tag. second value attribute. third boolean value tell whether checkbox checked or not when rendered initially. helpful in edit form show current selections.

note providing id attribute explicitly. if don't that, 3 checkboxes have same id (derived names), not valid html because id should not repeated.

generated html should this:

<li><input type="checkbox" name="campaign[channels][]" id="campaign_channels_folder" value="folder" /> folder</li> <li><input type="checkbox" name="campaign[channels][]" id="campaign_channels_fax" value="fax" /> fax</li> <li><input type="checkbox" name="campaign[channels][]" id="campaign_channels_email" value="email" /> email</li> 

the controller see channels param array of selected values.

this should work both new , edit forms.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -