Adding Twitter bootstrap styling to Rails form helpers - ruby-on-rails

After reading the answer which suggested I use Simple_form gem with bootstrap integration, I installed it and created my form according to simple_form instructions, but the input boxes are floating right.
This is the layout. The form is being called with the partial 'shared/reg'
<div class="container">
<div class="row">
<div class="span8"><%= yield %></div>
<div class="span4">
<%= render 'shared/reg' %>
</div>
</div>
</div>
This is my simple form form
<%= simple_form_for("user", :url => main_app.user_registration_path, :html => { :class => "form-horizontal" } ) do |f| %>
<%= f.input :name %>
<%= f.input :vote, :collection => [ "For", "Against", "Undecided"] %>
<%= f.input :country, :collection => [ "Canada", "Iceland", "Other"] %>
<%= f.input :email %>
<%= f.input :image, :as => :file %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.button :submit %>
<% end %>
Below you can see how the input boxes are floating right in relation to the submit button.
Update

Rather than using the .form-actions class, which wraps the submit button in gray block (which might not work for your page design), you can also wrap the button in a control group like this:
<div class="control-group">
<div class="controls">
<%= f.button :submit %>
</div>
</div>
This is only really needed if you're using the .form-horizontal class on the form itself.
If you're looking for a drop-in replacement form builder that outputs bootstrap-style markup for Rails, you might want to check out a gem that I put together to handle this sort of thing:
https://github.com/potenza/bootstrap_form
Here's how you'd setup a horizontal-style form with the submit button properly lined up:
<%= bootstrap_form_for(#user, html: { class: 'form-horizontal' }) do |f| %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.password_field :password_confirmation, label: 'Confirm Password' %>
<%= f.control_group do %>
<%= f.primary "Save User" %>
<% end %>
<% end %>
This is the example output:
<form accept-charset="UTF-8" action="/users" class="form-horizontal" id="new_user" method="post">
<div class="control-group">
<label class="control-label" for="user_email">Email</label>
<div class="controls">
<input id="user_email" name="user[email]" size="30" type="text" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="user_password">Password</label>
<div class="controls">
<input id="user_password" name="user[password]" size="30" type="password" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="user_password_confirmation">Confirm Password</label>
<div class="controls">
<input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" />
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" name="commit" type="submit" value="Save User" />
</div>
</div>
</form>

You should try the following:
<%= form_for("user", :url => main_app.user_registration_path, :html => { :class => "form-horizontal" } ) do |f| %>
<fieldset>
<legend>User Registration</legend>
<div class="control-group">
<%= f.label :name, class: "control-label" %>
<div class="controls">
<%= f.text_field :name %></p>
</div>
</div>
<div class="form-actions">
<%= f.submit %>
</div>
</fieldset>
Note that the bootstrap uses some specific selectors for some classes / html elements, so that if you forget to add an element or a class, everything else will be messed up... In this aspect there's no leeway.
On a side note, you should definitely try simple_form, and the bootstrap integration. It will make your life easier.
Update:
<%= simple_form_for("user", :url => main_app.user_registration_path, :html => { :class => "form-horizontal" } ) do |f| %>
<fieldset>
<legend>User Registration</legend>
<%= f.input :name %>
<%= f.input :vote, :collection => [ "For", "Against", "Undecided"] %>
<%= f.input :country, :collection => [ "Canada", "Iceland", "Other"] %>
<%= f.input :email %>
<%= f.input :image, :as => :file %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<div class="form-actions">
<%= f.submit %>
</div>
</fieldset>
<% end %>

Related

Customizing Devise sign_in authentication with bootstrap

I am using Devise for authentication, but I want to use my own form input.
I have checked that Devise's sign_in works. I was able to login with this on localhost:3000/users/sign_in :
<h2>Log in</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
I created on index page this form, but I could not get it to work (I was not able to login with the code below):
<form class="form-inline">
<div class="form-group">
<input type="email" class="form-control" id="user_email" placeholder="email?">
</div>
<div class="form-group">
<label for="exampleInputPassword2">/</label>
<input type="text" class="form-control" id="user_password" placeholder="password?">
</div>
<button type="submit" class="btn btn-primary">Sign In</button>
</form>
I read formhelper on rubyonrails.org website and bootstrap's css page but it still did not clear up how to use bootstrap's form.
Here are some of the things I don't understand:
The following code <input type="email" class="form-control" id="user_email" placeholder="email?">, what should I put for type= and id=? How can I tell bootstrap's form to submit a post request for my session? I can somewhat read and interpret Devise' form: <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> by looking at it: resource is :user, the url is being sent to user's session. I could not find information online about bootstrap's form. How can I integrate my own custom form using bootstrap with Devise' session?
Your problem has not really something to do with Bootstrap at first. I would try to get the form working, before you add bootstrap.
The from could be created like this:
<%= form_for(:user, :url => session_path(:user)) do |f| %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
<%= f.submit 'Sign in' %>
<%= link_to "Forgot your password?", new_password_path(:user) %>
<% end %>
Now you add some bootstrap flavor to this form:
<%= form_for(:user, :url => session_path(:user)) do |f| %>
<div class="form-group">
<%= f.text_field :email, placeholder: 'example#example.com' %>
</div>
<div class="form-group">
<%= f.password_field :password %>
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<%= f.submit 'Sign in', class: 'btn btn-primary' %>
<%# link_to "Forgot your password?", new_password_path(:user) %>
<% end %>
Most of the information I is from this page of the devise wiki.

devise edit form with bootstrap

In my edit view, I want to apply bootstrap. However, when replacing...
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="row">
<div class="col-md-6">
<div class="row form-group">
<div class="col-md-4"><%= f.label :username %></div>
<div class="col-md-8"><%= f.text_field :username %></div>
</div>
with this...
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label class="sr-only" for="username">Username</label>
<input type="text" class="form-control" name="username" placeholder="Username" value="<%= f.text_field :username %>">
</div>
</div>
</div>
I get this as output
I have tried many alternatives to include erb but don't understand the issue. How should I include the field in my edit forms?
you can do all the things with rails.
<div class="row">
<div class="col-md-4">
<div class="form-group">
<%= f.label :username %>
<%= f.text_field :username, class: "form-control", placeholder: "Username" %>
</div>
</div>
</div>
For more detail Please visit this link
http://apidock.com/rails/ActionView/Helpers/FormHelper/text_field
If you want to use add-on you can as below
<div class='input-group' >
<%= f.text_field :username, class: "form-control", placeholder: "Username" %>
<span class="input-group-addon" id="basic-addon2">#example.com</span>
</div>
Try this instead
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label class="sr-only" for="username">Username</label>
<%= f.text_field :username, class: "form-control", placeholder: "Username" %>
</div>
</div>
</div>
I would suggest using a gem called simple_form, it's made by the creators of Devise and works nicely with Bootstrap. It also has nice validation errors.
https://github.com/plataformatec/simple_form
Here's an example:
= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
.form-inputs
= f.input :email, required: false, autofocus: true, placeholder: 'Email'
= f.input :password, required: false, placeholder: 'Password'
= f.input :remember_me, as: :boolean, label: 'Remember Me' if devise_mapping.rememberable?
.form-actions
= f.button :submit, 'Sign in', class: 'btn-block btn-success'
%br
= link_to 'Forgot your password?', new_user_password_path
The code above will automatically add any bootstrap classes and labels...etc

How to generate a radio-inline label with simple_form and bootstrap?

I'm using:
<%= simple_form_for #visitor, html: {class: 'form-inline'} do |f| %>
<%= f.error_notification %>
<%= f.input :favorite, label: false, as: :radio_buttons %>
<%= f.submit "Choose!", :class => "btn btn-primary"%>
<% end %>
And I get this html:
<div class="control-group radio_buttons required visitor_favourite">
<div class="controls">
<label class="radio">
<input class="radio_buttons required" id="visitor_favourite_true" name="visitor[favourite]" type="radio" value="true" />
Yes
</label>
<label class="radio">
<input class="radio_buttons required" id="visitor_favourite_false" name="visitor[favourite]" type="radio" value="false" />
No
</label>
</div>
</div>
But what I'm looking for is a way to generate the labels as radio-inline because that is the style i want:
<label class="radio-inline">
There is a similar question here but for the "radio inline" class and desn't work as to generate radio-inline.
I'm using simple_form 3.0.2 and tried 3.1.0rc2 and Bootstrap 3.2.0 with rails 4.1.4
Anyway or method to solve this?
By getting inspiration form this answer I managed to do it by creating a :radio_buttons_inline input type.
Create the file:
app/inputs/radio_buttons_inline_input.rb
with this content:
class RadioButtonsInlineInput < SimpleForm::Inputs::CollectionRadioButtonsInput
def input
label_method, value_method = detect_collection_methods
#builder.send("collection_radio_buttons", attribute_name, collection, value_method,
label_method, input_options, input_html_options,
&collection_block_for_nested_boolean_style)
end
protected
def item_wrapper_class
"radio-inline"
end
end
And then use it in your view like so:
<%= simple_form_for #visitor do |f| %>
<%= f.error_notification %>
<%= f.input :favourite, :as => :radio_buttons_inline%>
<% end %>
This renders the html as:
<div class="control-group radio_buttons_inline required visitor_favourite">
<div class="controls">
<label class="radio-inline">
<input class="radio_buttons_inline required" id="visitor_favourite_true" name="visitor[favourite]" type="radio" value="true" />
Yes
</label>
<label class="radio-inline">
<input class="radio_buttons_inline required" id="visitor_favourite_false" name="visitor[favourite]" type="radio" value="false" />
No
</label>
</div>
</div>
I got the following to work under simple_form 3.1.0:
f.input :favorite, label: false, as: :radio_buttons,
item_label_class: "radio-inline", item_wrapper_tag: false
Here is for Bootstrap 4:
<div class="form-check form-check-inline">
<%= f.label :favorite, class: "form-check-label" do %>
<%= f.radio_button :favorite, true, false, class: "form-check-input" %> Yes
<% end %>
</div>
<div class="form-check form-check-inline">
<%= f.label :favorite, class: "form-check-label" do %>
<%= f.radio_button :favorite, false, false, class: "form-check-input" %> No
<% end %>
</div>
Just pass in:
item_wrapper_class: 'inline'
It'll look like:
<%= f.input :favorite, label: false, as: :radio_buttons, item_wrapper_class: 'inline' %>
And it should work.

How can I do a helper inside other helper?

I want make a helper how in this example
def textForm()
'<div class="form-group">
<%= f.label :nombre, :class => "col-md-3 control-label" %>
<div class="col-md-9">
<%= f.text_field :nombre, :class=> "form-control"%>
</div>
</div>'.html_safe
end
but... when I call this helper with <%= textForm %> just print html syntax.
apparently I need make something as "pre-render". Do you have any ideas?
Partial
As mentioned in the comments, you'll be better using a partial to sort this out:
#app/views/controller/_your_partial.html.erb
<div class="form-group">
<%= f.label :nombre, :class => "col-md-3 control-label" %>
<div class="col-md-9">
<%= f.text_field :nombre, :class=> "form-control"%>
</div>
</div>
This will allow you to call the partial as follows:
<%= render partial: "controller/your_partial" %>
--
Helper
If you want to call HTML directly from a helper, you should probably look at using the raw method:
<%= raw textForm %>
and then
#app/helpers/application_helper.rb
def textForm()
'<div class="form-group">
<%= f.label :nombre, :class => "col-md-3 control-label" %>
<div class="col-md-9">
<%= f.text_field :nombre, :class=> "form-control"%>
</div>
</div>'
end

Display error messages at the bottom of form submit

Is there a twitter boot strap class to display all validation error messages at the bottom of the form submit button?
<div class="row-fluid">
<div class="span12">
<div class="devise-body">
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name),
:html => { :class=>'control-group error'} ) do |f| %>
<%= f.error_notification %>
<div class="controls">
<div class="row-fluid">
<div class="span6">
<%= f.input :first_name,:required=>true,:autofocus => true %>
</div>
<div class="span6"><%= f.input :last_name,:required=>true%>
</div>
</div>
<div class="row-fluid">
<div class="span6"><%= f.input :company %></div>
<div class="span6"><%= f.input :title %></div>
</div>
<div class="row-fluid">
<div class="span6"> <%= f.input :email, :required => true %></div>
<div class="span6"><%= f.input :phone_number %></div>
</div>
<%= f.input :password, :required => true %>
<%= f.input :password_confirmation, :required => true %>
</div>
<div class="form-actions" >
<!-- #class="form-actions" -->
<%= f.button :submit, "Sign up" ,:class => 'btn'%>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
If I were you I'd use their alert classes:
http://twitter.github.com/bootstrap/components.html#alerts

Resources