相关疑难解决方法(0)

如何在Rails 4中使用attr_accessible?

attr_accessible 似乎不再适用于我的模型.

在Rails 4中允许批量分配的方法是什么?

ruby-on-rails ruby-on-rails-4

257
推荐指数
3
解决办法
14万
查看次数

创建新用户时ActiveModel :: ForbiddenAttributesError

我在Ruby中有这个模型,但它抛出了一个 ActiveModel::ForbiddenAttributesError

class User < ActiveRecord::Base
  attr_accessor :password
  validates :username, :presence => true, :uniqueness => true, :length => {:in => 3..20}
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, :uniqueness => true, format: { with: VALID_EMAIL_REGEX }

  validates :password, :confirmation => true
  validates_length_of :password, :in => 6..20, :on => :create

  before_save :encrypt_password
  after_save :clear_password

  def encrypt_password
    if password.present?
      self.salt = BCrypt::Engine.generate_salt
      self.encrypted_password= BCrypt::Engine.hash_secret(password, salt)
    end
  end

  def clear_password
    self.password = nil
  end
end
Run Code Online (Sandbox Code Playgroud)

当我运行这个动作

  def create
    @user = User.new(params[:user])
    if @user.save …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails rails-activerecord

219
推荐指数
5
解决办法
19万
查看次数

精彩的ActiveModel :: ForbiddenAttributesError

我一直在使用strong_params并尝试让对象创建通过.我有两个问题.

  1. 您如何找出导致问题的属性?
  2. 我在下面的代码中缺少什么?

让我们从错误开始,日志告诉我什么.

ActiveModel::ForbiddenAttributesError in JobsController#create
Run Code Online (Sandbox Code Playgroud)

只是为了咯咯笑,这里是我看不到非常有用的日志:

Started POST "/jobs" for 127.0.0.1 at 2013-12-17 22:03:59 +0000
Processing by JobsController#create as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"Ohq4lVqPVMpjzbZwCfJNYBL78TAcoC0WZLSmpCzMD3k=", "job"=>{"job_title"=>"Junior Rails Developer", "job_description"=>"A new position getig nsomethfins lansnana", "languages"=>["", "Rails", "Ruby"], "country_code"=>"AO", "job_type"=>"Full-Time", "job_salary"=>"30000", "job_level"=>"Junior"}, "commit"=>"Create Job"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Completed 500 Internal Server Error in 8ms

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
Run Code Online (Sandbox Code Playgroud)

有道理,但如果我看看我的创作:

def create
    binding.pry
    @job = Job.new(job_params)

    respond_to do |format|
      if …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

7
推荐指数
1
解决办法
342
查看次数