创建在控制台上工作但不在网站上工作 - ruby​​ on rails

时间:2013-10-17 23:30:33

标签: ruby-on-rails ruby bcrypt bcrypt-ruby

我有一个表单在我的应用程序中注册客户,在添加密码之前工作正常(bcrypt-ruby)。现在客户不会在提交表单时创建,但如果我使用完全相同的值通过控制台创建它可以正常工作。我没有得到任何错误消息,只是一个事务回滚(唯一奇怪的是未经许可的参数:密码,password_confirmation但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)

1 个答案:

答案 0 :(得分:0)

您需要将私有方法中的必需参数和允许参数添加到控制器中。

例如,在customers_controller.rb的底部:

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

如果您不希望将来输入那么长的东西,您也可以使用“bang”版本来允许模型上的所有属性。

private
    def customer_params
        params.require(:customer).permit!
    end