Restricting length of a form using simple_form? - ruby-on-rails

I'm using simple form for a basic login form for my site within the nav area at the top of the page.
Currently, it's spilling the form fields over two rows and no matter how many display: inline attributes I apply. It still refuses to go back onto one line.
What im trying to do is restrict the number of characters in the form so that the fields fit onto one line.
Here's my current form code..
<%= simple_form_for("user", :url => user_session_path, :html => {:id => "sign_in", :class => 'form-inline' }, :remote => true, :format => :json) do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.submit 'Login' %>
<%= link_to "Forgot your password?", new_password_path('user') %>
<% end %>

Easy way is to simply use the maxlength html attribute for inputs:
f.input :name, :input_html => { :maxlength => x }
But that will just restrict the number of characters that can be added into a single input. If your problem is with the width of those inputs, just use CSS to specify a custom width per input so the inputs won't overflow into a second line.

Related

Selected drop down menu in simple_form when f.association is present in the view

Can you please tell me if there is a way using simple_form with Rails, when you have "f.associations" present in a view, to disable the selected drop down menu in the form but still use the prefilled value of the field?
I have tried with option ":disabled => true" but it disables the whole text field and a I need a value to be present here. When I submit the form I receive an error that a value should be present.
I have tried with option ":as => :hidden", the input value does not appear, but when I submit the form I receive an error that a value should be present.
I have tried with option ":readonly => true", but drop down menu still appears. It appears grayed out but can still be selected.
Thank you,
Silviu
Use input hidden with same variable and value for f.assocation disabled, when submit form, the needing value will be sended at once time.
Sample code illustrate the case, note line f.association :company get the value company_id but not send, so the f.input :company_id will replace, and works!
Welcome to project railstrace !
<p>Simple Form content: </p>
<%= simple_form_for #user, url: save_path, :method => :post do |f| %>
<%= f.input :email %>
<%= f.association :company, disabled: true %>
<%= f.input :company_id, :as => :hidden, :input_html => {:value => #user.company_id} %>
<%= f.association :roles %>
<%= f.button :submit %>
<% end %>

How to send additional variable with rails form?

I am working with a rails form which takes users text input and sends it to the controller.
It sends two infos, the text and the language of the text (I18n.locale variable). My form looks something like that:
<%= form_for(:text, :url => {:action => 'create'} ) do |f| %>
<%= f.label :content, "#{t :"Write whatever you want"}" %><br />
<%= f.text_area :content, :cols => 80, :rows => 3 %> <br />
<%= f.hidden_field :locale, :value => I18n.locale %>
<%= f.submit "#{t :Post}"%>
<% end %>
I am sending that locale value using a hidden field. But I think this is a bad practice. User can easily modify this form. So is there any way to send that locale value among other form data automatically without any visible/hidden field?
If you want to avoid modifing data by user maybe you can use session like locale_session = "en", and use that session when you are dealing with data in controller
There are two ways of adding variable with rails form
<%= f.hidden_field :locale, :value => I18n.locale %>
AND
<%= form_for(:text, :url => {:action => 'create', :locale => I18n.locale} ) do |f| %>
But both of the above can easily modified by the user.
To avoid this either you have to use sessions or ssl page
Temporary solved this adding another line into controller. Not sure it it safe or not
def create
#text = Microblog.new(params[:text])
#text.update_attributes(params[#text.locale = "#{I18n.locale}" ])

Rails 3: Changing input box length

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

Disable a Text Box in Ruby on Rails?

I have the code:
<% generate_bullets = Bullet.all %>
<% generate_bullets.shuffle.first(4).each do |t| %>
<%= f.text_field, :bullets, :class => 'text_field disabled' %>
I want to disable a text box using embedded ruby, and am unable to do so. If I could receive any help on the situation I'm facing, it would be very greatly appreciated.
After I disable the text box I want to have a button generate four random ID's from the database table "bullets" and print them on the disabled text box in an array format, and utilize those four printed ID's to post them onto a created page. Any help with that would be even better.
Let me know if I'm reading this right: you're trying to disable the text field from the get-go in the HTML. Is that right?
If so, disabled isn't a class; it's its own attribute.
<%= f.text_field, :bullets, :class => 'text_field', :disabled => true %>
You can also use :readonly => true attribute.
For HAML
= f.text_field :name, :class => "form-control", :readonly => true
For ERB
<%= f.text_field :name, :class => "form-control", :readonly => true %>

Forms blank when rendering a partial when using a collection of objects. Help!

Alright, I know my title is a little obscure but it best describes the problem I am having.
Essentially, I have a list of users, and want to be able to edit their information in-line using AJAX.
Since the users are showing up in rows, I am using a partial to render the data and the forms (which will be hidden initially by the ajax), however, when the rows are rendered currently only the last item has it's form's fields populated.
I suspect this has something to do with the fact that all the form fields have the same id's and it is confusing the DOM. But I don't know how to make sure the id's are unique.
Here is a small example:
In my view:
<%= render :partial => 'shared/user', :collection => #users %>
My partial (broke down to just the form) note that I am using the local variable "user"
<% form_for user, :html => {:multipart => true} do |f| -%>
<%= f.label :name, "Name*" %>
<%= f.text_field :title, :class => "input" %>
<%= f.label :Address, "Address" %>
<%= f.text_field :address, :class => "input" %>
<%= f.label :description, "Description*" %>
<%= f.text_area :description, :class => "input" %>
<% end -%>
When the html is rendered each form has a unique id (for the id of the user) but the elements themselves all have the same id, and only the last user form is actually getting populated with values.
Does anyone have any ideas?? :)
Thanks in advance!
Alright, after having some lunch and regaining some brain cells, (and with a little help from Google) I figured this one out.
When passing a collection to a partial like this:
<%= render :partial => 'shared/user', :collection => #users %>
Rails creates a counter variable that you can use to define an index for the form in the form of "variable_counter":
<% form_for user, :index => user_counter, :html => {:multipart => true} do |f| -%>
This adds the index number to the form id as well as all the field id's and solved my little problem. :)
I hope this helps out someone else with this issue. :)

Resources