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.
Related
I'm using simple_form to create my scaffolding forms in my current project. I need some help to improve my address CRUD. After my db:seed there are lots of cities registered.
My current form for address is:
<%= simple_form_for #address, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :street%>
<%= error_span(#address[:street]) %>
<%= f.input :zip%>
<%= error_span(#address[:zip]) %>
<%= f.label :city_id %>
<%= f.collection_select(:city_id , City.all, :id, :name, prompt: true) %>
<%= error_span(#address[:city_id]) %>
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
address_path, :class => 'btn btn-default' %>
<% end %>
How can I filter my citties collection bellow selecting first the state ?
<%= f.collection_select(:city_id, City.all, :id, :name, prompt: true) %>
Thanks
Add this class method to your model:
def self.by_state
order('state DESC')
end
Then use it in your form:
<%= f.collection_select(:city_id , City.by_state, :id, :name, prompt: true) %>
I'm using 'rails-settings-cached' gem and I want to add ability of creating/updating settings in backend. Model has fields: 'var', 'value', etc... As form builder I'm using Formtastic-bootstrap.
When I doing f.input :value I get no implicit conversion of nil into String. I think that the reason is that 'value' is a keyword.
How can I solve my problem?
Thank you!
UPD:
<%= semantic_form_for [:admin, #setting], :html => {:class => "form-horizontal"} do |f| %>
<%= f.semantic_errors :var, :value%>
<%= f.inputs do %>
<%= f.input :var %>
<%= f.input :value, :as => :text %>
<% end %>
<%= f.actions :class => 'form-group' do %>
<div class="col-lg-offset-2 col-lg-8">
<%= f.action :submit %>
<%= f.action :cancel %>
</div>
<% end %>
Could someone please explain how to create a dynamically generated pull-down select that shows name labels to the web visitor and writes the corresponding ID to the database? This concept seems so basic that it must be obvious to everyone else but I am completely at wit's end trying to find a way to make this work and/or a good code example for learning how to do this. Any suggestions for a very frustrated newbie?
I have a simple category model using Awesome Nested List to track about 200 categories. These are the table fields:
t.string :name
t.integer :parent_id
t.integer :lft
t.integer :rgt
This is the category.rb model:
class Category < ActiveRecord::Base
acts_as_nested_set
attr_accessible :name, :parent_id
end
This is the Simple Form for view/categories/_form.html.erb
<%= simple_form_for(#category) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name, :label => 'Category name' %>
<%= f.select :parent_id, :label => 'Parent category', :value_method => { |r| [r.name, r.id, { :class => r.category.id }]}, :include_blank => true %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
This yields a "SyntaxError in Categories#new" error message
Extracted source (around line #6):
3:
4: <div class="form-inputs">
5: <%= f.input :name, :label => 'Category name' %>
6: <%= f.select :parent_id, :label => 'Parent category', :value_method => { |r| [r.name, r.id, { :class => r.category.id }]}, :include_blank => true %>
7: </div>
8:
9: <div class="form-actions">
You can use awesome_nested_set's view helper for building your select:
<%= f.select :parent_id, nested_set_options(Category, #category) {|i| "#{'-' * i.level} #{i.name}" } %>
I want to make chain selects part inside my Formtastic form. But is it possible to set custom ids for multiple selects for future AJAX replacement?
This doesn't work:
<%= semantic_form_for [:admin, #production_year] do |f| %>
<%= f.inputs do %>
<%= f.input :car_model, :label => "CarModel", :as => :select, :collection => Brand.find(:all, :order => "name ASC"), :id => "brand_id" %>
<% end %>
<% end %>
Options should be passed to the input_html hash, like so:
<%= f.input :car_model, :input_html => { :id => "brand_id" } %>
I'm Rails newbie.
How to make a form which allows user to choose a language(en,fr etc) through Radio buttons in Home#Index View to Submit to Home#Language action ?
Thanks in advance
<%= form_tag language_path, :method => :post do %>
<%= label_tag :language_english, 'English' %>
<%= radio_button_tag :language, 'english' %>
<%= label_tag :language_french, 'French' %>
<%= radio_button_tag :language, 'french' %>
<%= submit_tag %>
<% end %>
Where language_path is the path defined in your routes.rb, such as
match "/home/language" => "home#language", :as => 'language'