Jean
Jean

Reputation: 5411

form_for with a select helper using 2 models

I have two models offer and country they don't have any relationship, but in my new offer form, I want a select tag for select the country of the offer.

Here is my new action:

def new
    @countries = Country.all
    @offer = Offer.new
end

and this is my view

<%= form_for(@offer) do |f| %>
    <%= f.select @countries %> #I know this is wrong.
    <%= f.submit %>
<% end %>

Any idea.

Thanks

Upvotes: 1

Views: 110

Answers (1)

Matt
Matt

Reputation: 14048

If you need to select the country of an offer, that implies there should be a relationship in the models.

class Offer < ActiveRecord::Base
  belongs_to :country
end

class Country < ActiveRecord::Base
  has_many :offers
end

View:

<%= form_for :offer do |form| %>
  <%= form.collection_select :country_id, Country.all, :id, :name %>
  <%= form.submit %>
<% end %>

If this is not what you're after please refine your question.

Upvotes: 1

Related Questions