Rails collection_select + bootstrap dropdown - css

<div class="dropdown">
...
<%= ff.collection_select :vendor_id, #vendors, :id, :vendor, :include_blank => true, class: "dropdown-menu" %>
...
</div>
I have a form and then I'm using fields_for as shown above to collect input. But I'm unable to see the stylized bootstrap dropdown as illustrated here. What changes should I be making to see the bootstrap stylized dropdown?

from
<%= ff.collection_select :vendor_id, #vendors, :id, :vendor, :include_blank => true, class: "dropdown-menu" %>
to
<%= ff.collection_select :vendor_id, #vendors, :id, :vendor, {:include_blank => true}, {class: "dropdown-menu"} %>
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

Related

How can I style simple form f.association check_boxes elements?

I'm trying to style a simple quiz. The image that I use is supposed be the background of the checkbox and the value.name should be on top (as a header) with the value.description as text below. Right now everything obviously is displayed next to each other. And I can't figure out a way to change it.
<%= simple_form_for #user_value, :method => 'post' do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.association :value, label_method: lambda { |value| image_tag(cl_image_path value.photo.key) + "#{value.name} - #{value.description}"} , :label => "Select at least 10 values", as: :check_boxes, input_html: { class: "value-selector" }, item_wrapper_class: 'value-item'%>
</div>
<div class="form-actions">
<%= f.button :submit, "Continue" %>
</div>
<% end %>

How to customize devise login and signup forms created using the simple_form gem?

I am using devise for authentication in my rails app and I have created the forms using the simple_form gem. I want to customize the login and signup forms using css but can't seem to figure out to target, which class to use and add a line break.
here is what I need to do,
Add a line break after the label and move the asterisk after the label.
customize the input boxes using css
customize the signup button using css
This is how my devise view for signup page looks like,
<div class="formwrapper">
<center>
<h1>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :first_name, :label => 'First Name', required: true, autofocus: true %>
<br>
<%= f.input :last_name, :label => 'Last Name', required: true, autofocus: true %>
<br>
<%= f.input :user_name, :label => 'Username', required: true, autofocus: true %>
<br>
<%= f.input :email, :label => 'Email', required: true, autofocus: true %>
<br>
<%= f.input :password, :label => 'Password', required: true %>
<br>
<%= f.input :password_confirmation, :label => 'Confirm Password', required: true %>
<br>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</center>
</div>
Ok. This is simple to do.
In your code change this to
<%= f.input :first_name, :label => 'First Name', required: true, autofocus: true %>
THIS :-
<%= f.input :first_name, :label => 'First Name', required: true, autofocus: true , **:class => 'yourcustomclass'** %>
Similarly for the other controls.
Since Devise is an engine, all its views are packaged inside the gem. These views will help you get started, but after some time you may want to change them. If this is the case, you just need to invoke the following generator, and it will copy all views to your application:
rails generate devise:views
If you have more than one Devise model in your application (such as User and Admin), you will notice that Devise uses the same views for all models. Fortunately, Devise offers an easy way to customize views. All you need to do is set:
config.scoped_views = true
inside the config/initializers/devise.rb file.

How can I add multiple classes to a Rails form input with erb?

So this is what I want to do:
<% for x in 6..10 %>
<%= form_for(current_user.responses.new, html:{class: 'col-xs-2 col-md-1 col-lg-1 vcenter center'}) do |f, index| %>
<%= f.text_field :response_value, label: false, :class => "my-width <%= x %>" %>
<% end %>
<% end %>
But it doesn't work. I want to have two classes on the input field, one being a normal css class using html. The other using an erb variable, in this case 'x'.
I've tried
<%= f.text_field :response_value, label: false, :class => "my-width center vcenter", :class => x %>
Which only assigns the second :class, and overrides the first.
How can I assign both these classes ("my-width" and x) to the form input field?
Posting a new answer since Rails4 has a much better way of doing this:
<%= f.text_field :response_value, label: false, class: ["my-width", x] %>
<%= f.text_field :response_value, label: false, :class => "my-width <%= x %>"
Isnt valid ruby syntax. Im suprised your not getting an error.
Use string interpolation:
<%= f.text_field :response_value, label: false, :class => "my-width #{x}" %>
Also its worth noting you should use html classes that are solely a number.
Something like:
:class => "my-width-#{x}"
Is valid.
<%= f.text_field :response_value, label: false, :class => "my-width <%= x %>" %>
This line doesnt really make any sense, since you are putting an erb tag to the string inside an erb tag...
Try
<%= f.text_field :response_value, label: false, :class => "my-width #{x}" %>

Ruby on Rails: Required input field CSS not showing

I have a form and I'm setting a field to be required before submitting, however nothing is showing up when I hit the Search button. What do I need to do to style the form?
<%= form_tag search_path, :method => :get, class: "form-search-home" do %>
<%= text_field_tag :q, :class => "term form-control" %>
<%= text_field_tag :loc, :class => "loc form-control", :required => true %>
<%= button_tag :type => :submit, :class => "btn" do %>Search<% end %>
<% end %>
Thanks!
nothing is showing up when I hit the Search button
The problem here is likely a Rails / HTML issue than CSS (as mentioned in your question)
Syntax
As pointed out in the comments, you have a series of problems with your code syntax, specifically with submit_tag & text_field_tag:
<%= form_tag search_path, method: :get, class: "form-search-home" do %>
<%= text_field_tag :q, nil, class: "term form-control" %>
<%= text_field_tag :loc, nil, class: "loc form-control", required: true %>
<%= submit_tag "Search", class: "btn" %>
<% end %>
This should fix any of the syntax issues you have on your form, allowing it to submit. The reason why it doesn't at the moment is likely down to the syntax being incorrect. If you use the above code, it should render the form correctly, allowing you to submit it as required!
--
CSS
CSS is cascading style sheets - meaning they're only meant to style your page. They can't fix any syntax, backend or HTML rendering issues - only how the HTML appears in the browser
If you've still got trouble with your CSS, you'll be best styling the form with the inputs inheriting from the main class styling:
#app/assets/stylesheets/application.css
form {
/* form code */
}
form input.required {
/* required form element styling */
}
Does your form code generate a valid HTML?
As far as I see from documentation, text_field_tag method has three arguments:
text_field_tag(name, value = nil, options = {})
Your example ommits the second argument (value), so may be that is the case. Wonder if this could help:
<%= text_field_tag :loc, nil, :class => "loc form-control", :required => true %>

Rails: Simple_form on one line?

I am making a form with simple_form, and I'm trying to get all inputs on the same line (I want all elements to be inline horizontally on the rendered page).
I have googled the problem for some hours, but I couldn't find a solution that works.
The simple_form code:
<%= simple_form_for(#post, :html => {:class => 'form-inline' }) do |f| %>
<%= f.input :link, label: false, placeholder: "here..." %>
<%= f.input :type, as: :radio_buttons, collection: [['<span class="add-on"><i class="icon-on icon-white"></i></span>'.html_safe, '0'], ['<span class="add-on"><i class="icon-off icon-white"></i></span>'.html_safe, '1'], ['<span class="add-on"><i class="icon-on icon-white"></i> + <i class="icon-of icon-white"></i></span>'.html_safe, '2']], item_wrapper_class: 'inline', label: false %>
<%= button_tag(type: 'submit', class: "btn btn-inverse") do %>
<i class="icon-ok icon-white"></i>
<% end %>
<% end %>
Any solution to this?
simple_form creates a form containing a div for each input.
Each div contains a label and an input field
so your css should look like:
.form-inline div { display: inline-block }

Resources