0

我有一个表格可以在我的应用程序中注册客户,在添加密码内容(bcrypt-ruby)之前工作正常。现在,提交表单时不会创建客户,但如果我通过控制台使用完全相同的值创建它就可以了。我没有收到任何错误消息,只是事务回滚(唯一奇怪的是未经允许的参数:密码,密码确认,但如果重要,则为 idk)。我正在使用 protected_attributes gem 而不是现在的标准 strong_params。为什么在控制台上工作而不是通过 POST?一定是 bcrypt 的东西,或者我错过了一些非常愚蠢的东西,但我在我的研究中找不到任何答案。请帮助:s

模型:

attr_accessible :name, :surname, :email, :phone, :address, :password, password_confirmation

has_secure_password

控制器创建动作:

def create
  @customer = Customer.new(customer_params)

  respond_to do |format|
  if @customer.save
    format.html { redirect_to @customer, notice: 'Customer was successfully created.' }
    format.json { render action: 'show', status: :created, location: @customer }
  else
    format.html { render action: 'new' }
    format.json { render json: @customer.errors, status: :unprocessable_entity }
  end
end

结尾

形式:

<%= form_for :customer, :url => '/customers#new', remote: true do |f| %>
  <%= render '/shared/error_messages', object: f.object %>
  <p>
    <%= f.label(:name, "Nome") %>
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label(:surname, "Sobrenome") %>
    <%= f.text_field :surname %>
  </p>
  <p>
    <%= f.label(:email, "Email") %>
    <%= f.text_field :email %>
  </p> 
  <p>
    <%= f.label(:phone, "Telefone") %>
    <%= f.text_field :phone %>
  </p>
  <p>
    <%= f.label(:address, "Endereço") %>
    <%= f.text_field :address %>
  </p>
  <p>
    <%= f.label(:password, "Senha") %>
    <%= f.password_field :password %>
  </p>
  <p>
    <%= f.label(:password_confirmation, "Confirme sua senha") %>
    <%= f.password_field :password_confirmation %>
  </p>
  <%= f.submit "Enviar", class: "btn btn-large btn-primary" %>
<% end %>

服务器输出:

Started POST "/customers" for 127.0.0.1 at 2013-10-17 19:53:15 -0300
Processing by CustomersController#create as JS
  Parameters: {"utf8"=>"√", "customer"=>{"name"=>"Derpina", "surname"=>"Derpson", "email"=>"derpina@email.com", "phone"=>"2314534", "address"=>"Derpstreet, 123", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Enviar"}
Unpermitted parameters: password, password_confirmation
  ←[1m←[35m (0.0ms)←[0m  begin transaction
  ←[1m←[36mCustomer Exists (0.0ms)←[0m  ←[1mSELECT 1 AS one FROM "customers" WHERE LOWER("customers"."email") = LOWER('derpina@email.com') LIMIT 1←[0m
  ←[1m←[35m (0.0ms)←[0m  rollback transaction
  Rendered customers/_form.html.erb (6.0ms)
  Rendered customers/new.html.erb within layouts/application (9.0ms)
  Rendered shared/_error_messages.html.erb (0.0ms)
  Rendered layouts/_newCustomer.html.erb (3.0ms)
  Rendered layouts/_newRestaurant.html.erb (1.0ms)
  Rendered layouts/_header.html.erb (6.0ms)
  Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 314ms (Views: 65.0ms | ActiveRecord: 1.0ms)
4

1 回答 1

0

You need to add the required and permitted parameters in a private method to your controller.

For example, at the bottom of your customers_controller.rb:

private
    def customer_params
        params.require(:customer).permit(:name, :surname, :email, :phone, :address, :password, password_confirmation)
    end

If you don't want to type that long thing in the future, you can also use the "bang" version to permit all attributes on the model.

private
    def customer_params
        params.require(:customer).permit!
    end
于 2013-10-17T23:54:01.823 回答