Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML <form> tag causing Rails form to submit a GET instead of POST request

I have a form that works fine until I add the tags for styling (I am using twitter bootstrap). The app is Rails 3.1 and I am on a mac. Here is a form that works fine with no issues:

<%= form_for @user do |f| %>
<% if @user.errors.any? %>
  <div class="alert-message error">
    <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
  </div>
  <% end %>

  <div class="field">
    <%= f.label :email %>
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %>
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions"><%= f.submit %></div>
<%end %>

Then, when I add the tag to the same exact form like this:

<form class="form-stacked">
<%= form_for @user do |f| %>
<% if @user.errors.any? %>
  <div class="alert-message error">
    <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
  </div>
  <% end %>

  <div class="field">
    <%= f.label :email %>
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %>
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions"><%= f.submit %></div>
<%end %>
</form>

This form appears to issue a GET request instead of a POST request. Instead of redirecting me upon save like it does without the tag, I see this in the URL: http://localhost:3000/signup?utf8=%E2%9C%93&authenticity_token=Pt1vOp7lykCPFdj5BJeZ6xwJM2vy0JomMGSKoB%2FyYpU%3D&user%5Bemail%5D=test%40get.com&user%5Bpassword%5D=kevin&user%5Bpassword_confirmation%5D=kevin&commit=Create+User

I have tried specifying post in both the rails and HTML. Specifying it in rails using :method => post yields the same results (the GET request and URL info above). Using in the HTML looks like a POST request but the User object isn't saving (I belive this is the case because I am not getting redirected to the root_url, which is set in the controller and works fine when not using tag). Any ideas?

like image 820
kcurtin Avatar asked Sep 12 '25 21:09

kcurtin


1 Answers

you want this:

<%= form_for @user, :html => { :class => "form-stacked", :id => "something" } do |f| %>
  stuff goes in here as before
<% end %>
like image 54
stephenmurdoch Avatar answered Sep 15 '25 12:09

stephenmurdoch