gpt4 book ai didi

ruby-on-rails - 了解 cattr_accessor 在 Rails 模型类中的用法

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:32 25 4
gpt4 key购买 nike

我是 ruby​​ 和 rails 的新手。在我的 Rails 应用程序中,我正在尝试使用 Wicked Wizard gem,需要一些帮助来理解我在代码中遇到的 cattr_accessor

  create_table "pets", force: :cascade do |t|
t.string "name"
t.string "colour"
t.string "owner_name"
t.text "identifying_characteristics"
t.text "special_instructions"
t.datetime "created_at"
t.datetime "updated_at"
t.string "email"
t.string "password"
end

模型

 class Pet < ActiveRecord::Base
has_many :pet_photos
cattr_accessor :form_steps do
%w(identity characteristics instructions)
end

attr_accessor :form_step
validates :email, presence: true
validates :name, :owner_name, presence: true, if: -> { required_for_step?(:identity) }
validates :identifying_characteristics, :colour, presence: true, if: -> { required_for_step?(:characteristics) }
validates :special_instructions, presence: true, if: -> { required_for_step?(:instructions) }

def required_for_step?(step)
return true if form_step.nil?
return true if self.form_steps.index(step.to_s) <= self.form_steps.index(form_step)
end



end

      class Pet::StepsController < ApplicationController
include Wicked::Wizard
steps *Pet.form_steps

def show
@pet = Pet.find(params[:pet_id])
render_wizard
end

def update
@pet = Pet.find(params[:pet_id])
@pet.update(pet_params(step))

if params[:images]

params[:images].each do |image|
@pet.pet_photos.create(image: image)
end
end

render_wizard @pet
end

private

def pet_params(step)
permitted_attributes = case step
when "identity"
[:name, :owner_name]
when "characteristics"
[:colour, :identifying_characteristics]
when "instructions"
[:special_instructions]
end

params.require(:pet).permit(permitted_attributes).merge(form_step: step)
end

end

路线

  PetThing::Application.routes.draw do
resources :pets, only: [:new, :create, :index, :destroy] do
resources :steps, only: [:show, :update], controller: 'pet/steps'
end

root to: 'pets#index'

现在我的问题是

1) cattr_accessorattr_accessor 有什么区别?

       cattr_accessor :form_steps do
%w(identity characteristics instructions)
end
attr_accessor :form_step

2) 为什么两个不同的符号(:form_steps , :form_step)被用作cattr_accessor 的方法参数> 和 attr_accessor 方法?

3) 为什么要将 block 作为参数传递给 cattr_accessor 方法?

非常感谢任何帮助。谢谢

最佳答案

首先,此方法已弃用或移动。您使用哪个版本,Rails 4 还是 Rails?

cattr_accessor > 替换为 mattr_accessor(*syms, &blk)对于 Module这是类的父类(super class) Class .我推荐使用 attr_accessor,它只是一种为类设置属性的方法,它像类的 getter 或 setter 一样工作,但仅适用于实例(在内存中),属性是保存在任何地方。

cattr_accessor就像 attr_*方法,但对于类级别。你不会想到的一件事是因为它使用了支持 @@form_steps ,类和所有实例之间共享的值。

API文档:

为类属性定义类和实例访问器。

module HairColors
mattr_accessor :hair_colors
end

class Person
include HairColors
end

Person.hair_colors = [:brown, :black, :blonde, :red]
Person.hair_colors # => [:brown, :black, :blonde, :red]
Person.new.hair_colors # => [:brown, :black, :blonde, :red]

如果子类更改了值,那么父类的值也会更改。同样,如果父类更改值,那么子类的值也会更改。

class Male < Person
end

Male.hair_colors << :blue
Person.hair_colors # => [:brown, :black, :blonde, :red, :blue]

要选择退出实例编写器方法,请传递 instance_writer: false。要选择退出实例读取器方法,请传递 instance_reader: false。

module HairColors
mattr_accessor :hair_colors, instance_writer: false, instance_reader: false
end

class Person
include HairColors
end

Person.new.hair_colors = [:brown] # => NoMethodError
Person.new.hair_colors # => NoMethodError

或通过instance_accessor: false , 选择退出这两个实例方法。

module HairColors
mattr_accessor :hair_colors, instance_accessor: false
end

class Person
include HairColors
end

Person.new.hair_colors = [:brown] # => NoMethodError
Person.new.hair_colors # => NoMethodError

您还可以传递一个 block 来设置具有默认值的属性。

module HairColors
mattr_accessor :hair_colors do
[:brown, :black, :blonde, :red]
end
end

class Person
include HairColors
end

Person.class_variable_get("@@hair_colors") #=> [:brown, :black, :blonde, :red]

1)

  • attr_accessor:是一个 ruby​​ 方法,可以生成 getter 和 setter
  • cattr_accessor:是一个为类创建 getter 和 setter 的 ruby​​ 方法(很奇怪)。

2)类变量有从一个类到另一个类的倾向。 @@form_steps 类变量可以通过继承树暴露。

3) 设置哪些属性将拥有此类。 form_steps

p = Pet.new
p.form_steps = "var"

并使用form_steps当前实例 Pet 中的属性(就像您在方法 required_for_step? 中所做的那样)。

关于ruby-on-rails - 了解 cattr_accessor 在 Rails 模型类中的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47658029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com