簡體   English   中英

在 Rails 4 中如何使用 attr_accessible?

[英]How is attr_accessible used in Rails 4?

attr_accessible似乎不再適用於我的模型。

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

Rails 4現在使用強參數

現在,在控制器中完成保護屬性。 這是一個例子:

class PeopleController < ApplicationController
  def create
    Person.create(person_params)
  end

  private

  def person_params
    params.require(:person).permit(:name, :age)
  end
end

無需attr_accessible在模型中設置attr_accessible

處理accepts_nested_attributes_for

要使用帶有強參數的accepts_nested_attribute_for ,您需要指定哪些嵌套屬性應列入白名單。

class Person
  has_many :pets
  accepts_nested_attributes_for :pets
end

class PeopleController < ApplicationController
  def create
    Person.create(person_params)
  end

  # ...

  private

  def person_params
    params.require(:person).permit(:name, :age, pets_attributes: [:name, :category])
  end
end

關鍵字是不言自明的,但為了以防萬一,您可以在Rails Action Controller指南中找到有關強參數的更多信息。

注意 :如果您仍想使用attr_accessible ,則需要將protected_attributes添加到Gemfile 否則,您將面臨RuntimeError

如果你更喜歡attr_accessible,你也可以在Rails 4中使用它。 你應該像gem一樣安裝它:

gem 'protected_attributes'

之后,您可以在Rails 3中使用attr_accessible

此外,我認為這是最好的方法 - 使用表單對象來處理質量分配,保存嵌套對象,你也可以使用protected_attributes gem

class NestedForm
   include  ActiveModel::MassAssignmentSecurity
   attr_accessible :name,
                   :telephone, as: :create_params
   def create_objects(params)
      SomeModel.new(sanitized_params(params, :create_params))
   end
end

我們可以用

params.require(:person).permit(:name, :age)

如果person是Model,你可以在方法person_params上傳遞這個代碼並在create方法或者方法中代替params [:person]

1)更新設計,以便它可以通過將此行添加到您的應用程序的Gemfile來處理Rails 4.0:

gem 'devise', '3.0.0.rc' 

然后執行:

$ bundle

2)再次將attr_accessible的舊功能添加到rails 4.0

嘗試使用attr_accessible ,不要對此進行評論。

將此行添加到應用程序的Gemfile:

gem 'protected_attributes'

然后執行:

$ bundle

Rails 5的更新:

gem 'protected_attributes' 

似乎不再起作用了。 但是給:

gem'protected_attributes_continued'

一試。

我不得不將 Rails 應用程序從 3.2 遷移到 6.1,所以即使 gem 'protected_attributes' 也不是一個選項。 我很欣賞在控制器中使用 require().permit() 的參數,但我不想重新輸入或剪切並粘貼模型中的所有這些屬性,所以我決定改用這個初始化代碼(放在一個文件中)在配置/初始值設定項中):

# fix attr_accessible in an initializer
# wrap ActionController::Parameters code in singleton method defined
# from attr_accessible so controller code can call class method
# to get permitted parameter list
# e.g. model: class A < ActiveRecord::Base,
# controller calls A.permit_attr(params)
# lots simpler than moving all attr_accessible definitions to controllers
# bug: fails if more than one attr_accessible statement

def (ActiveRecord::Base).attr_accessible *fields
  puts "attr_accessible:"+self.name+":permitted_params fields=#{fields.inspect}"
  define_singleton_method("permit_attr") { |params|
    # may have subclasses where attr_accessible is in superclass
    # thus must require by subclass name so should calculate require at runtime
    rq = self.name.downcase.to_sym
    puts "...permit_attr:self=#{rq} permit(#{fields.inspect})"
    params.require(rq).permit(fields)
  }

end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM