Reputation: 63
I'm probably using this incorrectly and I cant figure it out.
I am using an options_for_select on my simple_form. It renders fine with no errors, but the selected option does not save to the database. All other fields save no problems.
The select_tag is
<%= select_tag :experiment_type, options_for_select(['AOV', 'Conversion']), :prompt => "Select a Project Type" %>
Controller:
class ExperimentsController < ApplicationController
before_action :find_advertiser
before_action :find_experiment, only: [:edit, :update, :show, :destroy]
def index
@experiments = Experiment.all.order("created_at DESC")
end
def show
end
def new
@experiment = Experiment.new
@advertisers = Advertiser.all.map{ |c| [c.name, c.id] }
end
def create
@experiment = Experiment.new(experiment_params)
@experiment.advertiser_id = params[:advertiser_id]
if @experiment.save
redirect_to advertiser_path(@advertiser)
else
render 'new'
end
end
def edit
@projects = Project.all.map{ |c| [c.name, c.id] }
end
def update
@experiment.advertiser_id = params[:id]
if @experiment.update(experiment_params)
redirect_to experiment_path(@experiment)
else
render 'edit'
end
end
def destroy
@experiment.destroy
redirect_to root_path
end
private
def experiment_params
params.require(:experiment).permit(:advertiser_id, :name, :experiment_type, :hypothesis, :priority, :status, :launch_date,
:description, :baseline_url, :test_url, :baseline_aov_60, :baseline_aov_30, :baseline_aov_mtd,
:baseline_conversion_60, :baseline_conversion_30, :baseline_conversion_mtd)
end
def find_advertiser
@advertiser = Advertiser.find(params[:advertiser_id])
end
def find_experiment
@experiment = Experiment.find(params[:id])
end
end
Upvotes: 1
Views: 3321
Reputation:
I would need your full form layout to tell you for sure, but according to your experiment_params method, experiment_type field is a part of experiment. However, when you use just select_tag it is not connected to your main object. You need to use just select
. Similar to this:
<%= simple_form_for :experiment do |f| %>
...
<%= f.select ... %>
...
<% end %>
or in the simple_form format:
<%= f.input :experiment_type, collection: ['AOV', 'Conversion'] %>
My guess is also based on your hash:
"experiment_type"=>"AOV", "experiment"=>{"name"=>"Test" ....
The experiment_type is outside of your "experiment".
Upvotes: 2