rails'form_tag'默认为POST方法,但使用GET方法提交

时间:2014-05-13 17:01:28

标签: ruby-on-rails forms httprequest

我有这样的表格:

<%= form_tag(controller: :orders, action: :create_user) do %>

  <%= label_tag 'first_name', 'First Name' %>
  <%= text_field_tag 'first_name' %><br/>

  <%= label_tag 'last_name', 'Last Name' %>
  <%= text_field_tag 'last_name' %><br/>

  <%= label_tag 'email', 'Email' %>
  <%= email_field_tag 'email' %> <br />

  <%= label_tag 'username', 'Username' %>
  <%= text_field_tag 'username' %> <br/>

  <%= label_tag 'password', 'Password' %>
  <%= password_field_tag 'password' %><br />

  <%= label_tag 'password_confirmation', 'Confirm Password' %>
  <%= password_field_tag 'password_confirmation' %><br />

  <%= submit_tag 'Create' %>

<% end %>

create_user控制器中的orders操作如下所示:

def create_user
  hydra = Typhoeus::Hydra.hydra

  post_user = Typhoeus::Request.new(
    "localhost:3006/api/v1/users.json",
    method: :post,
    headers: { Accept: "application/json" },
    body: params.except(:authenticity_token).to_json
  )

  post_user.on_complete do |response|
    if response.success?
      user = User.from_json(response.body)
      @user = user.user

      respond_to do |format|
      format.html {redirect_to controller: :orders, action: :account_created, user_id: @user.id}
      end

    elsif response.timed_out?
      logger.debug "got a time out"

    elsif response.code == 0
      logger.debug(response.return_message)

    else
      # Received a non-successful http response.
      logger.debug("HTTP request failed: " + response.code.to_s)
    end
  end
  hydra.queue post_user
  hydra.run
end

我在config/routes.rb

中有这个
post '/orders/users/create_user', to: 'orders#create_user'

当我提交表单时,我收到此错误:

No route matches [GET] "/orders/users/create_user"

我想知道为什么应用程序使用GET提交表单或此GET请求的来源。当我使用浏览器检查表单源时,表单标记如下所示:

<form accept-charset="UTF-8" action="/orders/users/create_user" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="toYBO4DzVYK30M4uiBRtxCIZ2OSR/g1kMe4qrTzrmQM=" /></div>

  <label for="first_name">First Name</label>
  <input id="first_name" name="first_name" type="text" /><br/>

  <label for="last_name">Last Name</label>
  <input id="last_name" name="last_name" type="text" /><br/>

  <label for="email">Email</label>
  <input id="email" name="email" type="email" /> <br />

  <label for="username">Username</label>
  <input id="username" name="username" type="text" /> <br/>

  <label for="password">Password</label>
  <input id="password" name="password" type="password" /><br />

  <label for="password_confirmation">Confirm Password</label>
  <input id="password_confirmation" name="password_confirmation" type="password" /><br />

  <input name="commit" type="submit" value="Create" />

</form>

GET请求来自哪里?

1 个答案:

答案 0 :(得分:1)

您可以直接使用路由及其http请求,试试这个

<%= form_tag( '/orders/users/create_user', :method => :post ) do %>

的routes.rb

post "/orders/users/create_user" => "orders#create_user", :as => :create_user

match "/orders/users/create_user" => "orders#create_user", :via => :post, :as => :create_user