提交表单时不发送任何帖子参数

时间:2013-11-22 20:07:35

标签: ruby-on-rails forms form-helpers

我有一个名为AlphaUser的模型。当我提交表单来创建一个新的alpha用户时,我被告知params [:post]中没有任何数据。怎么会发生这种情况?

我的表格代码:

<%= form_for :alpha_user,
    url: alpha_users_path,
    class: "form",
    id: "alphaTest" do |f| %>
    <div class="form-group">
        <%= f.label :email %>
        <%= f.text_field :email, 
            class: "form-control", placeholder: "grandma@aol.com" %>
        <%= f.text_field :producer,
          class: "hidden", value: "0" %>
    </div>
    <%= f.submit "Request Access", class: "btn btn-primary" %>
<% end %>

我的控制器:

class AlphaUsersController < ApplicationController  
    def create
        render text: params[:post].inspect
    end

end

仍然收到数据,只是采用以下格式:

{"utf8"=>"✓", "authenticity_token"=>"blahblahblah", "alpha_user"=>{"email"=>"test@test.com", "producer"=>"0"}, "commit"=>"Request Access", "action"=>"create", "controller"=>"alpha_users"}

2 个答案:

答案 0 :(得分:0)

检查此答案:Custom name for params hash from Rails form_for

此命名约定也是有原因的。通过第7节“了解参数命名约定”来查看here

答案 1 :(得分:0)

您需要为:alpha_user调用params ...例如PARAMS [:alpha_user]。

如果您在Rails 4中使用strong_parameters,这应该可以解决问题:

class AlphaUsersController < ApplicationController  

  def create
    @alpha_user = AlphaUser.new(alpha_user_params)
    @alpha_user.save
  end

  private
    def alpha_user_params
      params.require(:alpha_user).permit(:email, :producer)
    end

end

请注意,在您的创建操作中,您不再直接引用params,而是强选参数所需的alpha_user_params方法。