Rails表单发出的是GET请求而不是POST请求

5
我正在制作一个Rails 3.1应用程序,有一个注册表单曾经正常工作,但我似乎改变了一些东西以破坏它...我正在使用Twitter bootstrap和twitter_bootstrap_form_for gem。我进行了一些更改,搞乱了表单字段的格式,更重要的是,当我提交注册表单以创建新用户时,信息显示在URL中,看起来像这样:

编辑:这在最新版本的Chrome和Firefox中发生

http://localhost:3000/?utf8=%E2%9C%93&authenticity_token=UaKG5Y8fuPul2Klx7e2LtdPLTRepBxDM3Zdy8S%2F52W4%3D&user%5Bemail%5D=kevinc%40example.com&user%5Bpassword%5D=testing&user%5Bpassword_confirmation%5D=testing&commit=Sign+Up

这是表单的代码:

    <div class="span7">
      <h3 class="center" id="more">Sign Up Now!</h3>
        <%= twitter_bootstrap_form_for @user do |user| %>
      <%= user.email_field :email, :placeholder => 'me@example.com' %>
      <%= user.password_field :password %>
      <%= user.password_field :password_confirmation, 'Confirm Password' %>
          <%= user.actions do %>
        <%= user.submit 'Sign Up' %>
      <% end %>
        <% end %>
   </div>

这里是UsersController的代码:

    class UsersController < ApplicationController
      def new
       @user = User.new
      end

      def create
        @user = User.new(params[:user])
        if @user.save
         redirect_to about_path, :notice => "Signed up!"
        else
         render 'new'
        end
       end
     end

我不确定你是否需要更多内容,如果有,请告诉我!谢谢!

编辑:为了调试,我尝试指定:post并使用普通的form_for。

     <%= form_for(@user, :method => :post) do |f| %>
         <div class="field">
            <%= f.label :email %>
            <%= f.email_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 "Sign Up" %></div>
      <% end %>

这让我遇到了与上述相同的问题。
添加routes.rb文件:
    Auth31::Application.routes.draw do

     get "home"     => "pages#home"
     get "about"    => "pages#about"
     get "contact"  => "pages#contact"
     get "help"     => "pages#help"
     get "login"    => "sessions#new",     :as => "login"
     get "logout"   => "sessions#destroy", :as => "logout"
     get "signup"   => "users#new",        :as => "signup"
     root :to       => "pages#home"

     resources :pages
     resources :users
     resources :sessions
     resources :password_resets
    end
2个回答

4
问题在于我指定了HTML表单标签,而Rails form_for会自动生成它自己的表单标签。请参考以下示例将类添加到一个表单中:
<%= form_for(@user, :html => { :class => "form-stacked"} )  do |f| %>

这个很好用。我仍然不确定为什么有2个`
`标签会生成GET请求。

0

看起来你的for正在发送一个GET请求,而不是POST请求。我不确定为什么会发生这种情况(你是否尝试使用纯粹的form_for进行调试?),但你应该能够通过显式设置方法来修复它:

<%= twitter_bootstrap_form_for(@user, :method => :post) do |user| %>

我尝试使用:post指定,但无济于事。我也尝试使用普通的form_for。如果您不介意,请在上面添加代码作为编辑并查看。仍然存在相同的问题... - kcurtin
没问题,我做了一些修改,因为我意识到我听起来有点刻薄 :) - kcurtin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接