1

我是 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_accessor和有什么区别attr_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)为什么将块作为参数传递给cattr_accessor方法?

非常感谢任何帮助。谢谢

4

1 回答 1

4

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

cattr_accessor> 替换为mattr_accessor(*syms, &blk)哪个Module是类的超类Class。我建议使用attr_accessor,它只是为类设置属性的一种方法,它的工作方式类似于类的 getter 或 setter,但仅适用于实例(在内存中),属性保存在任何地方。

cattr_accessor就像attr_*方法一样,但是对于类级别。您不会想到的一件事是因为它使用了 backing @@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

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

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: 是一个 ruby​​ 方法,它为类创建一个 getter 和一个 setter(很奇怪)。

2) 班级变量有在班级之间徘徊的趋势。@@form_steps 类变量可以通过继承树公开。

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

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

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

于 2017-12-05T17:55:10.037 回答