Select_tag not saving associated child-object

17 views
Skip to first unread message

Josh Sr.

unread,
Jun 3, 2015, 4:01:59 PM6/3/15
to rubyonra...@googlegroups.com
I have a form with a select_tag and options_from_collection_for_select
that I can't seem to get to pass. The parent object, "Campaign", saves
but does not associate with the child-object "Uploadzip".

Campaign has_one Uploadzip
Uploadzip belongs_to Campaign

There's also the campaign_id foreign_key on the Uploadzips table.

Here's my Campaign controller:

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
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(:uploadzip,
:uploadpdfs).find(params[:id])
end

private

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


....and the form...


<%= form_for @campaign, url: {action: "create"} do |f| %>

.....some typical fields..

<%= f.label :data_file, class: "right-label" %>

<%= select_tag "uploadzip[campaign_id]",
options_from_collection_for_select(
Uploadzip.all, :id, :file_name
), { include_blank: "Include a Zip File" } %>

.....some more typical fields

<% end %>


Any help would be highly appreciated!

--
Posted via http://www.ruby-forum.com/.

Josh Sr.

unread,
Jun 3, 2015, 5:52:02 PM6/3/15
to rubyonra...@googlegroups.com
Member of a FaceBook group helped me figure it out by adding a little
extra logic in the controller save.

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

This was met with changing the select_tag's name to..

<%= select_tag :uploadzip_id,
options_from_collection_for_select(
Uploadzip.all, :id, :file_name
), { include_blank: "Include a Zip File" } %>

Reply all
Reply to author
Forward
0 new messages