I have a Campaign
model with a channel
column. This channel
will store a serialized
array of chosen results via checkboxes.
Here's the 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 Successfully Launched!"
redirect_to @campaign
else
flash[:error] = "There was a problem launching your 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 the form with checkboxes..
views/campaigns/_target.rb
<%= form_for @campaign, url: {action: "create"} do |f| %>
...
<label class="right-inline">
<%= f.label :channel, "Distribution Channel", class: "right-label" %>
</label>
<ul class="channel-list">
<% ["Folder", "Fax", "Email"].each do |channel| %>
<li><%= check_box_tag :channel, channel %> <%= channel %>
<% end %></li>
</ul>
...
<% end %>
I'm having problems saving these results inside the Campaign object.
Any help is highly appreciated.
each
loop is wrong. In each loop you create a<li>
and you have only one</li>
out of the loop. – Bydgoszcz