I have two model classes: Goal and Objective. A Goal has many Objectives, and an Objective belongs to one Goal.
I have created a page for creating a new Objective via a form. In the page’s controller, I am setting the value of a variable @default_goal like so:
@objective = Objective.new
if params.has_key?(:default_goal)
@default_goal = Goal.find(params[:default_goal])
end
Then, in my form I want to make that variable the default Goal in an association dropdown list. I tried the following but it doesn’t seem to work:
<%= simple_form_for @objective do |f| %>
<%= f.association :goal, :prompt => "Select a goal", :default => @default_goal %>
<% end %>
How can it be done?
Ah, the answer was really obvious. Instead of setting the default in the view form, it should be done on the record itself in the controller. Example:
That will automatically select it as the default option in the view form’s association list.