Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Use Rails’ form_for but set custom classes, attributes on <form> element?

form_for seems to ignore any 'extra' attributes like a data-foo attribute or class passed as options in its second argument.

= form_for @user, {:url => 'foo', :class => 'x', 'data-bar' => 'baz' } do |f|
 # ...

The output is a <form> tag with no x class or data-bar attribute.

What’s the fix?

Or, how can I grab a FormBuilder instance without using form_for?

Answer*

Draft saved
Draft discarded
Cancel
4
  • 3
    Wow, really? those should be exactly equivalent. You can reproduce this? Which version of Ruby are you using? Commented Oct 29, 2014 at 4:01
  • @Alan H. Using Ruby 2.1.3p242 = form_for(:user, :url => login_path, html: {:class => 'login_form'}) do |f| %> This was the only way that my code would apply the class, otherwise it was just ignoring it. Commented Oct 30, 2014 at 0:07
  • Those two lines are exactly identical. Guessing you forgot to save in between? Commented Jan 29, 2015 at 14:05
  • I know, they should be. For me it just wasn't working though. Saving wasn't the issue. Commented Jan 30, 2015 at 3:50

lang-rb

AltStyle によって変換されたページ (->オリジナル) /