I have a rails 4 app with simple form and bootstrap.
I have checkbox elements in my form. I want to align the check box to be vertically aligned with the content in other fields. At the moment the check box is further left than the rest of the content in other fields.
For example:
<%= f.input :experiment, :as => :boolean, :label => false, inline_label: 'Do you want experiment logs or methods?' %>
Does anyone know how to make the vertical alignment uniform so that checkboxes are not out to the left of the rest of the form elements?
Thank you
To expand on the above,
I'd like the checkbox to be left aligned in line with the left edge of the other form elements. At the moment, it is further left (like there is some kind of negative margin on that check box element).
I'd like all three of these field inputs to be flush at the left alignment. At the moment, the check box in the two boolean elements is further left than the third text field:
<%= f.input :survey, :as => :boolean, :label => false, inline_label: 'Do you need survey responses?' %>
<br><br>
<%= f.input :survey_link, label: 'Where is your survey?', :label_html => { :class => 'question-data' }, placeholder: 'Include a link to your survey', :input_html => {:style=> 'width: 650px; margin-top: 20px', class: 'response-project'} %>
<br>
<%= f.input :experiment, :as => :boolean, :label => false, inline_label: 'Do you want experiment logs or methods?' %>
As doc suggested here http://montrezorro.github.io/bootstrap-checkbox/ You should have data-label in your input so change your input like this:
<%= f.input :experiment, :as => :boolean, :label => false, :input_html => { :'data-label' => 'Do you want experiment logs or methods?' } %>
If you want prepend then :
<%= f.input :experiment, :as => :boolean, :label => false, :input_html => { :'data-label-prepend' => 'Do you want experiment logs or methods?' } %>
I made a custom wrapper like this:
config.wrappers :inline_checkbox, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.wrapper tag: 'div', class: 'col-sm-10 col-sm-offset-2' do |ba|
ba.wrapper tag: 'div', class: 'checkbox' do |baa|
baa.use :input
end
#ba.use :input
ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
In my case I wanted a simple inline checkbox with the label to the right to place below another form element. You may need to play with the col-sm-10 col-sm-offset-2. This goes in your SimpleForm initializer (or make a separate one line I did to avoid breaking things in the original). Also you need to restart your app to see the changes take effect.
Related
I am using Rails 3.1x, SimpleForm 2.1, and Bootstrap 2.2.x, and I want to change the position of the hint text on some of my forms.
Currently if I use code such as the following (this is a simplified version)
<%= simple_form_for #user, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :name, :hint => 'this should be your first name' %>
..
..
I will get a form looks like this
However, I would like it to look like this.
Any thoughts?
I am not sure its works well but try this , in your form field add class
<%= f.input :name, :hint => 'this should be your first name' , :class => "someclass"%>
and in application.css
.somecalss
{
width:somevalue;
}
In my horizontal form, all of my input elements are aligning properly except for the file inputs.
They take 100% width and are floated to the left.
In my simple_form_bootstrap.rb my wrapper seems to configured properly with the label taking 3 and input taking 9.. but somehow in the generated html, it's not taken into account.
Wrapper:
config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.use :label, class: 'col-sm-3 control-label'
b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
ba.use :input
ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
Form header:
<%= simple_form_for(#post, html: { class: 'form-horizontal', role: "form", multipart: true },
wrapper: :horizontal_form,
wrapper_mappings: {
check_boxes: :horizontal_radio_and_checkboxes,
radio_buttons: :horizontal_radio_and_checkboxes,
file: :horizontal_file_input,
boolean: :horizontal_boolean
}) do |f| %>
<%= f.error_notification %>
Form inputs in my form
<%= f.fields_for :assets do |builder| %>
<%= builder.input :attachment, as: :file, :label => "Image:" %>
<% end %>
Generated html
<div class="form-group file optional post_assets_attachment">
<label class="file optional control-label" for="post_assets_attributes_0_attachment">
Image:</label>
<input class="file optional form-control" id="post_assets_attributes_0_attachment" name="post[assets_attributes][0][attachment]" type="file">
</div>
How come the simple_form settings don't apply to the file inputs?
Found my answer in the simple-form 3.1.0.rc1 sample application.
Looks like specifying the wrapper in the form header didn't work for some reason. Had to apply it directly to the input.
I changed:
<%= builder.input :attachment, as: :file, :label => "Image:" %>
to
<%= builder.input :attachment, as: :file, :label => "Image:", wrapper: :horizontal_file_input %>
And it works perfectly now.
I'm working with a very large Ruby on Rails app, so I won't be able to include much of the source code. If I miss anything needed for the solution, please let me know, and I'll see if I can find it.
The part of the app I'm working on is the sign up page. We're using haml for the view, and bootstrap for styling. Here's the view code that's in a _form.html.haml partial:
= simple_form_for(resource, :as => resource_name,:class=>"", :url => registration_path(resource_name)) do |f|
.row-fluid
= f.input :email, :as => :string, :input_html => { :class => 'span12', :type => 'email', :required => 'required' }
.row-fluid
.span6
.input-append{ "data-role" => "acknowledge-input" }
= f.input :firstname, :as => :string, :label => 'First Name', :input_html => { :class => 'span12', :type => 'text', :required => 'required', :placeholder => "Letters Only Please" }
.span6
= f.input :lastname, :as => :string, :label => 'Last Name', :input_html => { :class => 'span12', :type => 'text', :required => 'required', :placeholder => "Letters Only Please" }
.row-fluid
.span6
= f.input :password, :as => :password, :input_html => { :class => 'span12', :type => 'password', :required => 'required' }
.span6
= f.input :password_confirmation, :as => :password, :input_html => { :class => 'span12', :type => 'password', :required => 'required' }
.row-fluid
.span7
%small
= link_to 'Already a Member?', new_user_session_path
.span5
= f.submit "Sign up", :class=>"btn btn-success btn-block"
I've got it set up (through bootstrap) so that it will outline each text box in red if the conditions for that particular variable are not met, and at the bottom of the page is a Sign Up button. The main problem we're having is that if you madly click on the Sign Up button, the system will happily create several copies of the user in the database. Ideally, the Sign Up button should disable itself after the first click (until the page is refreshed). I know there's a disabled option provided by Bootstrap, but I'm not sure how to trigger that only after the button has been clicked once.
As a corollary, I'd really like it if the button was actually disabled until all the front-end validation was done. But that's a bonus, not the main issue. ^_^
EDIT
As #rlecaro2 mentioned below I would really prefer something to happen when I click the button. As an initial attempt, I've added the following code:
- if submit_clicked?
= f.submit "Sign up", :class=>"btn btn-success btn-block", :disabled => 'disabled'
- else
= f.submit "Sign up", :class=>"btn btn-success btn-block", :onclick => "click_submit"
The two method are Ruby methods I've put in application_helper.rb:
def click_submit
#submit_clicked = true
end
def submit_clicked?
#submit_clicked
end
Unfortunately, this doesn't seem to work at all. I can click the button multiple times still. I suspect it has something to do with onclick wanting a JavaScript method. How do I have a separate Ruby method run when I click the button? It has to happen before the form is sent, because the whole point of this is that I only want the form to be sent once.
I'm guessing you use javascript for your front-end validations, in that case you could start up with the button disabled and enable it at the end of said validations.
Or better, don't display the button until you're ready to sign up, and make it disappear after clicked.
Both effects can be achieved using Jquery in a pretty straight-forward manner.
You should do this on the view's javascript file (under assets).
I'm out of time but check out this detailed
answer.
GL & HF.
The code below creates a form and styles the "submit" button according to some css ("button"). The problem is, when the page renders, it shows the normal rails submit tag button on top of the customized "button" css. How do I mute or disable the visual aspects of the rails submit tag button while still making it submit the form?
=form_tag new_site_url, :method => :get do
=text_field_tag :homepage,'', type: "text", class: 'text'
%button
=submit_tag "GO!"
Could you do this :
=form_tag new_site_url, :method => :get do
=text_field_tag :homepage,'', type: "text", class: 'text'
=submit_tag "GO!", class: 'button'
and set the css style for the button?
It better to do this :
=form_tag new_site_url, :method => :get do |f|
=f.text_field '', type: "text", class: 'text'
=f.submit "GO!", class: 'button'
Another way is (rails 4.1)
<%= submit_tag("Submit", :class => "btn btn-warning" ) %>
Here is where you go to find answers http://api.rubyonrails.org/
and if you are working in form_for you would do
<%= f.submit("Submit", class: "btn btn-default" ) %>
I'm using old school ruby(1.8.7) and rails(2.3.5)
heres what my submit tags look like for custom css styling :
<%= submit_tag("Edit", :style => "width:30px;") %>
where "Edit" is the text that appears on the button, and "width:30px;" is my styling. you can also cascade the stylings :
<%= submit_tag("Edit", :style => "width:30px;color:blue;") %>
You can add a style key to the hash
<p><%= submit_tag l(:button_apply), :class => 'btn btn-default btn-sm', :name => 'submit', :style => 'margin-left: 42px' %></p>
been trying to style my button_to button, however with no luck.
I've had a look in the CSS file and used
#button {color:red;}, button {color:red;} and .button {color:red;} but to no avail for all of them.
I also have this in my HTML.ERB file <%= button_to 'Compare DB',:action => "compare", :id => 'button' but that doesn't help.
Is there any reason why that doesn't work?
The CSS stylesheet is linked to the Rails app, as I tried changing the value for the table so that's not the issue.
Been trying for a while now, still can't figure out the solution.
You can use style inline
<%= button_to 'Compare DB',:action => "compare", :id => 'button', {:style => "background: color:red; } %>
or you can use css clas like
<%= button_to 'Compare DB',:action => "compare", :id => 'button', {:class => "button"} %>