Reputation: 11
I have models with references:
Vacation
name:string
slug:string:uniq
start_at:datetime
end_at:datetime
Transportation
name:string
Destinations
vacation:references
transportation:references
name:string
full_address:string
latitude:float
longitude:float
position:integer
I am wondering how can I add a reference in a form submission? I have a form working:
<%= form_for(@vacation) do |f| %>
<%= f.label :vacation %><br>
<%= f.text_area :name %>
<%= f.submit "Create" %>
when I submit, it now shows up on the home page. I want to pick a vacation from a select box (I'm close to handling that), like vacation with ID=2, and on form submission, tie this new destination to the selected vacation: ID=2. My working example looks like:
def create
@vacation = Vacation.new(vacation_params)
if @vacation.save
redirect_to '/'
else
render 'new'
end
end
private
def vacation_params
params.require(:vacation).permit(:name)
end
Thank you
Upvotes: 1
Views: 64
Reputation: 6260
I think what you are after is a select_tag:
You could add this line inside your form (you are going in the right track), so you select an existing vacation:
<%= select_tag "vacation_id", options_from_collection_for_select(Vacation.all, "id", "name") %>
And the structure of your tables should be as we discussed below the question.
Happy to have helped.
Upvotes: 1