How to add styling to a single input in a form_for
Asked Answered
D

2

8

I'm using the rails framework with HAML and I have bootstrap setup. How would I format the field inputs seperately. I want the input field for name to be 60% of the screen float left and the input for price to be 25% of the screen and float right.

I guess i'm asking how do i add classes to single inputs in a form_for. Thanks

= form_for @product,:url => products_path, :html => { :id => "fileupload", :multipart => true } do |f| 
  %p
    = f.label :name
    = f.text_field :name # i want to format this
  %p
    = f.label :price
    = f.text_field :price
Dennie answered 6/7, 2013 at 20:59 Comment(0)
Z
26

you can add a class to any form helper and use CSS to format it:

= f.text_field :name, class: 'your_class' # i want to format this

of cause you can also set a style option directly, but it is recommended to separate content and styling. So don't

= f.text_field :name, style: 'float: left; width: 60%; display: block' # i want to format this
Zosima answered 6/7, 2013 at 21:8 Comment(3)
Thanks for the answer! I can't mark you correct for another 3 minutes.Dennie
for simple_form use: , input_html: {style: 'direction: ltr;'}Androcles
better: for simple_form use: , input_html: {class: 'your_class'}Zosima
B
0
<%= f.password_field :password, class: "your style class", style: "display: block;" %>
Bischoff answered 15/11, 2023 at 19:35 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Hester
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Triley

© 2022 - 2024 — McMap. All rights reserved.