Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set class attribute with form_tag in rails

I have the following line of haml:

=form_tag :action => 'create', :controller => 'comments', :class => 'comment_form' do

But the html that gets output is:

<form accept-charset="UTF-8" action="/comments?class=comment_form" method="post"></form>

I want to set the class. How do I do this?

<-- Update -->

With this:

=form_tag ({ :action => 'create', :controller => 'comments' }, { :class => 'comment_form' }) do

I get this error:

 syntax error, unexpected ',', expecting ')'
...', :controller => 'comments' }, { :class => 'comment_form' }...

<-- Second Update -->

The problem above is the space between 'form_tag' and '(' @woahdae's answer is correct

like image 318
John Avatar asked Feb 01 '12 04:02

John


3 Answers

form_tag takes 2 options hashes, the first being passed to url_for, the second being passed to the form builder.

So, you have to do it like:

= form_tag({:action => 'create',...}, {:class => 'comment_form'}) do 

otherwise Rails thinks all the key/value pairs are for url_for, which will append any keys it doesn't understand as query parameters.

like image 70
Woahdae Avatar answered Sep 22 '22 09:09

Woahdae


This works for me:

form_tag named_route, :method => :put, :class => 'disable_on_submit' 

With Rails 3.0.15

like image 24
gamov Avatar answered Sep 24 '22 09:09

gamov


On Rails 5, you can do the following:

<%= form_tag(your_named_path, {class: 'form-inline'}) do %>

<% end %>
like image 36
pastullo Avatar answered Sep 21 '22 09:09

pastullo