HTML< form>标记导致Rails窗体提交GET而不是POST请求 [英] HTML <form> tag causing Rails form to submit a GET instead of POST request

查看:137
本文介绍了HTML< form>标记导致Rails窗体提交GET而不是POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作正常的表单,直到我为样式添加标签(我正在使用twitter bootstrap)。该应用程序是Rails 3.1,我在Mac上。这是一个可以正常工作但没有问题的表单:

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>

此表单似乎发出GET请求而不是POST请求。我没有重新定向保存,就像没有标记一样,我在URL中看到:
http://本地主机:3000 /注册UTF8 =%E2%9C%93&安培; authenticity_token = Pt1vOp7lykCPFdj5BJeZ6xwJM2vy0JomMGSKoB%2FyYpU%3D&安培;用户%5Bemail%5D =测试%40get.com&安培;用户%5Bpassword%5D =凯文&安培;用户%5Bpassword_confirmation%5D =凯文&安培;提交=创建+用户

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

我曾尝试在rails和HTML中指定post。在rails中使用:method => post指定它会得到相同的结果(上面的GET请求和URL信息)。在HTML中使用看起来像一个POST请求,但用户对象不保存(我相信这是因为我没有被重定向到root_url,这是在控制器中设置的,并且在不使用标签时工作正常)。任何想法?

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?

推荐答案

您需要:

you want this:

<%= form_for @user, :html => { :class => "form-stacked", :id => "something" } do |f| %>
  stuff goes in here as before
<% end %>

这篇关于HTML&lt; form&gt;标记导致Rails窗体提交GET而不是POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆