44

I need to add some class names to my form. I'm using Rails' form_for helper to do this. I've tried adding { :class => 'classname' } to no avail.

How can I add classes to this Rails helper output?

Alec Rust
  • 10,532
  • 12
  • 48
  • 63
James
  • 6,471
  • 11
  • 59
  • 86

3 Answers3

92

Try this:

form_for @order, :html => {:class => "foo"}
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
7

For Rails 4.0 here is the example from the guides:

http://guides.rubyonrails.org/form_helpers.html

1.2 Multiple Hashes in Form Helper Calls

form_tag(controller: "people", action: "search", method: "get", class: "nifty_form")
# => '<form accept-charset="UTF-8" action="/people/search?method=get&class=nifty_form" method="post">'
Daniel
  • 7,006
  • 7
  • 43
  • 49
2

Ruby Code Below,

<%= form_for :article, :html => { :class => "article-form" } do |form| %>
    <p>
        <%= form.label :title %>
        <%=
        form.text_field :title,
            placeholder: "A******'s title"
        %>
    </p>
    <p>
        <%= form.label :text %>
        <%=
        form.text_field :text,
            placeholder: "A******'s content"
        %>
    </p>
    <p>
        <%= form.submit %>
    </p>
<% end %>

HTML Output Below,

<form class="article-form" action="/articles/new" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="BRBjb0maKb5kNbGXV0wwoLeFUV6El2la/sOwweWhyN+nEIOK2Wam5Xz8PasZtpwvmiCF7yIO8Pab/OR2uoeZQA==">
    <p>
        <label for="article_title">Title</label>
        <input placeholder="A******'s title" type="text" name="article[title]" id="article_title">
    </p>
    <p>
        <label for="article_text">Text</label>
        <input placeholder="A******'s content" type="text" name="article[text]" id="article_text">
    </p>
    <p>
        <input type="submit" name="commit" value="Save Article">
    </p>
</form>
XY L
  • 25,431
  • 14
  • 84
  • 143