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.
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'} %>
Related
I am getting a weird error after adding <%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %> inorder to add a datetime selector.
I am wondering, what could the problem be?
NoMethodError in Messages#new
and
undefined method ``schedule' for #<Message:0x000000066fa1e8>
my create controller is
def create
#lists = current_user.lists.all
#message = Message.new(params[:message])
lists = params[:message][:lists]
schedule = params[:message][:schedule]
if #message.save
if schedule == [""]
MessageWorker.perform_async(#message.id, lists, current_user.id)
else
Schedule.create(execution_time: schedule, lists: lists, message_id: #message_id, user_id: current_user.id)
end
else
render action: "new"
flash[:notice] = "Messages Not Sent"
end
end
and the view is;
<%= simple_form_for(#message, :html => {:multipart => true}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :from, placeholder: "Max 11 characters" , input_html: {id: 'sms_from'} %>
<%= f.input :to, :input_html => {:value => params[:to], :class => "to"}, placeholder: "Separate Numbers with Comma", hint: "Include atleast one number in this section. Separate the numbers with commas." %>
<div id="add_lists"> <span class="button"> Add List </span></div>
<div id="all_lists"> <%= f.collection_check_boxes :lists, #lists, :id, :name, as: :select %></div>
<%= f.input :message, :as => :text, :input_html => {:cols => 60, :rows => 7, id: "compose_message", placeholder: 'Type your message here!'} %>
<div>
<p>
<span id="char_count">160 characters left</span>
<span id="number_of_messages">1 message(s)</span>
</p>
</div>
<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>
<%= f.input :user_id, :as => :hidden, :input_html => {:value => #current_user.id} %>
<%= f.input :status, :as => :hidden, :input_html => {:value => "Queued"} %>
<%= f.button :submit, "Send Message" %>
<% end %>
and the model top looks like this;
class Message < ActiveRecord::Base
attr_accessible :message, :phone, :status, :to, :from, :user_id, :schedule
validates :message, :presence => true
validates :from, :presence => true
validates :to, :presence => true
validates :status, :presence => true
validates_length_of :message, :maximum => 1600, :allow_blank => true
validates_length_of :from, :maximum => 11, :allow_blank => false
belongs_to :user
Everything was working well until i added;
<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>
Did you migrate the database to add the schedule column?
Or if you are not storing :schedule in the database, then you need to add:
attr_accessor :schedule
to your Message model.
You don't have to use f.input, you can use f.date_field of f.datetime_field as outlined in the API: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-date_field
Also, you don't have to use the form helper in this way as you can use datetimefield_tag instead of f.datetime_field as outlined here http://rubydoc.info/docs/rails/ActionView/Helpers/FormTagHelper:datetime_field_tag
I have a simple form:
<%= f.input :type, :required => true, :collection => ["Nonprofit","School","Company"], :hint => "Note: nonprofits will need to provide proof of nonprofit status", :input_html => { :value => params['type'] } %>
<%= f.input :name, :label => "Organization" %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
A user gets to this page through a url like http://www.website.com/org/signup?type=Company
I can use this format to enter a value into a field like name or email, but can't figure out how to pass the param to the drop down.
I've already tried a few things including changing :value to :selected or :option but nothing seems to work.
Alright, figured it out! Posting here for future use.
<%= f.input :type, :required => true, :collection => ["Nonprofit","School","Company"], :hint => "Note: nonprofits will need to provide proof of nonprofit status", :selected => params['type'] %>
The trick is to drop the :input_html part and just use
:selected = > params['type']
Hope that helps someone in the future!
I have two models, Category and Post.
Category.rb
class Category
include Mongoid::Document
field :title, :type => String
has_many :posts, :autosave => true, dependent: :destroy
end
Post.rb
class Post
include Mongoid::Document
field :title, :type => String
belongs_to :category
end
I'm using simple_form gem
If I write in my post form the next:
<%= simple_form_for(#post) do |f| %>
<%= f.collection_select :category, Category.all, :id, :title, :prompt => "Choose a Category"%>
<%= f.input :title %>
<%= f.button :submit %>
<% end %>
The form does works fine :).
but if I use the next form with simple_form format:
<%= simple_form_for(#post) do |f| %>
<%= f.association :category, :prompt => "Choose a Category" %>
<%= f.input :title %>
<%= f.button :submit %>
<% end %>
I get the next error:
Completed 500 Internal Server Error in 23ms
ActionView::Template::Error (undefined method `valid_options' for nil:NilClass):
How can I fix it?
Thank you!
The problem was fixed. Thank you to Carlos Antonio da Silva :D.
you can find the fix in http://groups.google.com/group/plataformatec-simpleform/browse_thread/thread/f384f0445af8468e or:
<%= f.input :category, :collection => Category.all, :prompt => "Choose a Category" %>
Thank you!
I need some help with handling validations with Twitter Bootstrap properly. The validations are working properly, the issue is Twitter Bootstrap Flash screen. This is what I mean:
No fields filled out and submitted returns: http://i.stack.imgur.com/8pvUc.png
The 2 of the required 4 fields submitted returns: http://i.stack.imgur.com/J6lCi.png
(notice that it doesn't flash the errors)
I have a basketball app where a Player can be on many Rosters(for player roster archiving purposes) and a Roster can have many Players.
Roster.rb
class Roster < ActiveRecord::Base
belongs_to :team
has_many :rosterizes
has_many :players, :through => :rosterizes
accepts_nested_attributes_for :players
validates_presence_of :jersey_number, :class_year
attr_accessible :jersey_number, :class_year, :players, :team_id, :players_attributes
end
Rosterizes.rb(poorly named I know...)
class Rosterize < ActiveRecord::Base
belongs_to :player
belongs_to :roster
attr_accessible :player_id, :roster_id
end
Player.rb
class Player < ActiveRecord::Base
has_many :rosterizes
has_many :rosters, :through => :rosterizes
validates_presence_of :first_name, :last_name
attr_accessible :first_name, :last_name, :active
end
_add_player_form.html.erb
(nested form)
<%= simple_nested_form_for #roster, :url =>player_added_team_path, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.simple_fields_for :players, #roster.players.build do |x| %>
<%= x.input :first_name %>
<%= x.input :last_name %>
<%= x.input :active, :as => :hidden, :input_html => {:value => true} %>
<%end%>
<%=f.input :class_year %>
<%=f.input :jersey_number %>
<%=f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%>
<div class="well">
<%= f.button :submit, :class => 'btn-primary icon-plus-sign btn-success', :value => "Add To Team" %>
</div>
<%end%>
Thanks for the help in advance!
SOLUTION
My problem is that every time the view was loaded, it was building a new form, thus never getting back the errors on the rollback
I made a change to my controller that builds for the nested form
Controller
def add_player
...
#new_player = #roster.players.build
end
And made the change accordingly to the view
_add_player_form.html.erb
<%= f.simple_fields_for :players, #new_player do |x| %>
...
<%end%>
This did the trick, thanks Mober!
the reason why only the first level validation is shown an the associated not is fairly simple... You do a <%= f.simple_fields_for :players, #roster.players.build do |x| %> which buildes the association ALWAYS new when the sites's rendered. What you want to is building the association only if it does not exists yet...
<% #roster.players.build if !#roster.players %>
<%= f.simple_fields_for :player do |player_form| %>
...
<% end %>
Twitter Bootstrap doesn't actually validate anything, it just provides styling for validation classes (error, success, etc).
Now, I'm not a Ruby expert by any means but it looks like you are ending your form early:
<%= f.simple_fields_for :players, #roster.players.build do |x| %>
<%= x.input :first_name %>
<%= x.input :last_name %>
<%= x.input :active, :as => :hidden, :input_html => {:value => true} %>
<%end%> <---- here
<%=f.input :class_year %>
<%=f.input :jersey_number %>
<%=f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%>
Should be this?
<%= f.simple_fields_for :players, #roster.players.build do |x| %>
<%= x.input :first_name %>
<%= x.input :last_name %>
<%= x.input :active, :as => :hidden, :input_html => {:value => true} %>
<%= f.input :class_year %>
<%= f.input :jersey_number %>
<%= f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%>
<% end %>
I'm starting to use simple_form for a rails application, and while converting some of my forms, I came across one that has two models that it is working with, sort of an embedded form. Is this possible with simple_form?
<% simple_form_for :topic, :url => forum_topics_path do |t| %>
<%= t.input :name, :label => 'Topic' %></p>
<p>First Post:<br/></p>
Title: <%= text_field :post, :title %> <--- this is where i start having problems
Body: <%= text_area :post, :body %>
<%= t.submit 'Save' %>
Thanks
Use simple_fields_for :
<%= simple_form_for :topic, :url => forum_topics_path do |topic_builder| %>
<%= topic_builder.input :name, :label => 'Topic' %>
<%= topic_builder.simple_fields_for :post do |post_builder| %>
<p>First Post:</p>
<%= post_builder.input :title, :input_html => { :size => 30 } %>
<%= post_builder.input :body, :as => :text, :input_html => { :rows => 20, :cols => 50, :class => 'resizable' } %>
<% end %>
<%= topic_builder.submit 'Save' %>
<% end %>
Notes
Note the = symbol in <%= simple_form_for ... and <%= simple_fields_for (required in Rails 3.x)
Removed "Title:" and "Body:" text. Use the label generated for the inputs and style their location with CSS as needed.
Added example of using input_html
There's another approach that I'm using and it works great. Ryan Bates (RailsCasts) has created a gem to handle this.
See https://github.com/reu/simple_nested_form for the details.