Reputation: 759
I'm using simple_form for my forms and I have a landing page which I have associated to a template. I'm trying to have a dropdown appear on the landing page form that lets the user select the template for the landing page and have the template names appear in the dropdown.
Here are my models: landing_page.rb
class LandingPage < ActiveRecord::Base
belongs_to :template
has_many :leads
accepts_nested_attributes_for :template
validates_presence_of :name
validates_presence_of :title
validates_presence_of :page_url
validates_presence_of :template_id
validates_presence_of :content
end
template.rb
class Template < ActiveRecord::Base
has_many :landing_pages
validates_presence_of :template_name
end
Here is my landing page form:
<%= simple_form_for(@landing_page) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
<%= f.input :title, :input_html => {:maxlength =>50, :style=> 'width: 500px'}, label: 'HTML Title' %>
<%= f.input :page_url, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
<%= f.input :template_id, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
<%= f.input :content, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
</div>
If I enter an integer for the template_id it's saved to the database correctly. However, I've tried every page I could find with examples and can't seem to make a dropdown (collection select) work for this field and have it display the template names in the dropdown.
I've looked at these pages: http://simple-form.plataformatec.com.br/ https://github.com/plataformatec/simple_form How to have a collasped drop down list in rails simple_form https://github.com/plataformatec/simple_form/wiki/Nested-Models
Any help is greatly appreciated.
Upvotes: 0
Views: 773
Reputation: 759
I got this to work:
<%= f.association :template, label_method: :template_name, value_method: :id, include_blank: true, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
Upvotes: 0