Using a new/unchanged installation of simple_form 3.0.0.rc on rails 4.0.0.rc1, this view code
<%= simple_form_for #order do |f| %>
<%= f.input_field :email %>
<%= end %>
produces this output
<input class="string email optional" id="order_email" maxlength="255" name="order[email]" size="255" type="text" />
but I had expected the output not to include maxlength and to set type to 'email', much like the #input method does:
<input class="string email optional" id="order_email" name="order[email]" type="email" />
My expectations come from the fact that simple_form.rb includes the following default values:
b.use :html5
b.optional :maxlength
What do I need to do to make the input attributes from #input_field default to the same as #input?
Input field helper method will take hash you pass to it in second argument and turn them info html attributes. Look at the code below, should do the trick:
<%= simple_form_for #order do |f| %>
<%= f.input_field :email, :type => :email, :maxlength => nil %>
<% end %>
According to the docs input_field method takes all the options as input_html option except :as, :collection, :label_method, :value_method keys. I tried adding :as => :email but no avail. But you could use :type => :email to get type="email" in the rendered html. And according to the source of the method it uses some defaults as well.
So for getting the email field:
<%= simple_form_for #order do |f| %>
<%= f.input_field :email, :type => :email %>
<% end %>
I think its related to your database field... I think you have set the databasefield as string, which hast 255 maximum length in general.. This could be the reason why it adds 255 automatically?
Related
I have tested three ways of doing a field required, first with no gem, just the usual form_for and it did work well, but I need some good gem for making easier adding fields to insert associations, then I installed the Simple Form. Here is the code I am using:
<%= simple_form_for #post do |p| %>
<%= p.text_field :title, :required => true %> <br />
<%= p.input :content, required: true%> <br />
<%= p.input :category_id, input_html: { required: true }%>
<%= p.submit %>
<% end %>
See how I used all the three ways of getting required to true and the usual way of creating a text field of the form_for so I can see if I find a solution. No success. Even after making config.browser_validations = true in config/initializers/simple_form.rb. Why is it working for form_for but not when I am using gems? I also tried Formtastic and had the same issue.
If you place required: true in the input you should see the field has the "required" class and required="required" attribute.
I'm using simple_form, and I wonder if it's possible to skip any wrapper divs when dealing with an association select.
Thank's
If you're using something like f.association :product you can remove both the generated label and wrapper like so: f.association :product, label: false, wrapper: false
https://github.com/plataformatec/simple_form#stripping-away-all-wrapper-divs
SimpleForm also allows you to strip away all the div wrappers around
the field that is generated with the usual f.input. The
easiest way to achieve this is to use f.input_field.
Example:
simple_form_for #user do |f|
f.input_field :name
end
Produces:
<input class="string required" id="user_name" maxlength="100"
name="user[name]" size="100" type="text" value="Carlos" />
To view the actual RDocs for this, check them out here - http://rubydoc.info/github/plataformatec/simple_form/master/SimpleForm/FormBuilder:input_field
Or ...
Do something like
config.wrappers :small do |b|
b.use :placeholder
b.use :label_input
end
and use it in this way:
# Specifying to whole form
simple_form_for #user, wrapper: :small do |f|
f.input :name
end
https://github.com/plataformatec/simple_form#configuration
use collection_select instead, in haml:
= f.collection_select :position_id, Position.all, :id, :name, {}, { class: 'span3' }
this example assumes you have a position model and want to add span3 as a class on the <select> it generates
I see myself writing a lot of :size => nil for f.text_field as so:
<%= f.text_field :street_address, :size => nil %>
<%= f.text_field :post_code, :size => nil %>
<%= f.text_field :city, :size => nil %>
Which is just dumb. Without the :size => nil above, text_field renders an <input> with a size="some number" (usually size="30") which I don't need or want there.
So, how can I implement DRY and make it so that f.text_field would not generate the size=30 or size="some number" attribute by default? This way I can avoid always having to type :size => nil.
All default field options are stored in one hash. It defaults to the following:
# action_view/helpers/form_helper
DEFAULT_FIELD_OPTIONS = { "size" => 30 }
You can delete "size" from it in an initializer, for example.
ActionView::Helpers::InstanceTag::DEFAULT_FIELD_OPTIONS.delete("size")
Rails extends Object class with with_options method. You can take advantage of it:
<%= form_for :foo do |f| %>
<% f.with_options :size => nil do |f_nil| %>
<%= f_nil.text_field :street_address %>
<%= f_nil.text_field :post_code %>
<%= f.text_field :city %> <!-- you can use old f here too! -->
<% end %>
<% end %>
Gives:
<input id="foo_street_address" name="foo[street_address]" type="text" />
<input id="foo_post_code" name="foo[post_code]" type="text" />
<input id="foo_city" name="foo[city]" size="30" type="text" /> <!-- you can use old f here too! -->
Try encapsulating the text_field like so:
def no_size_text_area(form, method) form.text_area(method, {:size => nil}) end
put the function in your helper file.
and using no_size_text_area in its place like so:
no_size_text_area(f,:city)
The Rails Team has since removed these defaults as of version 4.0.x. There is no way to set global defaults anymore either. The only easy solution is to use CSS to set the dimensions.
Using Rails 3 with Twitter Bootstrap and Simple_form, I am having issues changing the length of the input box in this field:
<div class="input-prepend input-append">
<%= f.input :price, :wrapper => :append do %>
<span class="add-on">$</span>
<%= f.input_field :price %>
<span class="add-on">.00</span>
<% end %>
</div>
Others say to add this after the :price variable:
:input_html => {:size => 15}
The 'do' loop seems to change the rules, any suggestions?
try
:style => "width: 100px;"
Twitter bootstrap has css classes for this. Depending on what size you want you can add class input-min, input-small, input-large and so on. You can also use the span classes, e.g. span1, span2, etc.
<div class="input-prepend input-append">
<%= f.input :price, :wrapper => :append do %>
<span class="add-on">$</span>
<%= f.input_field :price, :class => 'input-medium' %>
<span class="add-on">.00</span>
<% end %>
</div>
I am using the f.input form to create controls with labels in a :class => 'form-horizontal' form, using a class attribute or style attribute (directly or as a hash, any way I tried) didn't work for me, and had zero effect on the generated HTML.
This is what worked for me:
<%= f.input :description, input_html: { class: 'span12' } %>
This works with both the Bootstrap column layout classes ('span1', 'span2', etc,) the input sizing classes ('input-large', 'input-xxlarge', etc,) or whatever custom class you want. The trick is using the input_html key. You can also mess with the label using the label_html key but that's likely to mess up the form-horizontal layout.
It looks like the size key in :input_html => {**:size** => 15} is ignored by SimpleForm... when I tired this it did not affect the HTML output.
I found this here in the SimpleForm docs:
https://github.com/plataformatec/simple_form#usage
What's the simplest way in Ruby-on-Rails to create several simple hidden fields with known values and the same name in a number of non-model forms (form_remote_tag in my case, but I'm guessing that isn't relevant)?
By "simple hidden field", I mean one where the name is just a single string (field_name) rather than part of an array (field_name[]), so that the value can be read simply from the params hash as params[:field_name] rather than params[:field_name][0].
I have found that
<% form_remote_tag :url => {:action => "do_act"} do %>
<%= hidden_field :field_name, 0, :name => "field_name", :value => "foo" %>
<%= submit_tag "Submit" %>
<% end %>
produces an acceptable element (<input id="field_name_0" name="field_name" type="hidden" value="foo" />), but if I omit the :name parameter then the rendered field has the name field_name[0]. Omitting the 0 obviously causes really odd behaviour.
<%= hidden_field_tag :field_name, "foo" %> produces an acceptable element if there's only one such form, but generates HTML warnings (duplicate IDs) if there are more than one.
Is there a way to do this (barring defining a helper) in fewer arguments?
I would use hidden_field_tag and set the ID manually based on some value that is different for each form. Like this:
<%= hidden_field_tag :field_name, 'value', :id => 'field_name_' + unique_value %>
Where unique_value can be anything at all. If these forms have some sort of parent record that they refer to, it could be the ID of the parent. I assume that's why you have multiple similar forms on the same page in the first place.
You can simple pass the ID as an option. The method (form_tag_helper.rb) is defined as:
def hidden_field_tag(name, value = nil, options = {})
text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
end
So writing:
<%= hidden_field_tag :field_name, "foo", :id => "hidden_field_1" %>
<%= hidden_field_tag :field_name, "bar", :id => "hidden_field_2" %>
Produces:
<input id="hidden_field_1" name="field_name" type="hidden" value="foo" />
<input id="hidden_field_2" name="field_name" type="hidden" value="bar" />
Try hidden_field_tag:
<%= hidden_field_tag :field_name, "foo" %>