Rails表单发出GET请求而不是POST请求 [英] Rails form issuing GET request instead of POST request

查看:169
本文介绍了Rails表单发出GET请求而不是POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Rails 3.1应用程序,并具有可以正常运行的注册表单,但是我似乎已经更改了一些东西来打破它..我正在使用Twitter bootstrap和twitter_bootstrap_form_for gem.我进行了一些更改,使表单字段的格式混乱,但是更重要的是,当我提交注册表单"以创建新用户时,该信息将显示在URL中,如下所示:

I am making a Rails 3.1 app and have a signup form that was working fine, but I seemed to have changed something to break it.. I'm using Twitter bootstrap and twitter_bootstrap_form_for gem. I made some change that messed with the formatting of the form fields, but more importantly, when I submit the Sign Up form to create a new User, the information is showing up in the URL and looks like this:

这是在最新版本的Chrome和Firefox中发生的

This is happening in the latest versions of Chrome and Firefox

这是表格的代码:

    <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的代码:

Here is the code for the 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

不确定是否还需要更多,但如果需要,请告诉我!谢谢!

Not sure if there is more you need but if so let me know! Thank you!

为进行调试,我尝试指定:post并也使用普通的form_for

For debugging I tried specifying :post and also using a plain 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 %>

这给了我与上面相同的问题.

This gives me the same problem as above.

添加路线.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

推荐答案

问题是当Rails form_for生成自己的HTML表单标签时,我正在指定HTML表单标签.参见下文,将类添加到表单:

The problem was that I was specifying the HTML form tag when Rails form_for generates its own. See below to add a class to a form:

<%= form_for(@user, :html => { :class => "form-stacked"} )  do |f| %>

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

This worked fine. I'm still not sure why having 2 tags would generate a GET request.

这篇关于Rails表单发出GET请求而不是POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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