- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 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) 为什么要将 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
,类和所有实例之间共享的值。
为类属性定义类和实例访问器。
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)
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/
可不可以命名为MVVM模型?因为View通过查看模型数据。 View 是否应该只与 ViewModelData 交互?我确实在某处读到正确的 MVVM 模型应该在 ViewModel 而不是 Mode
我正在阅读有关设计模式的文章,虽然作者们都认为观察者模式很酷,但在设计方面,每个人都在谈论 MVC。 我有点困惑,MVC 图不是循环的,代码流具有闭合拓扑不是很自然吗?为什么没有人谈论这种模式: mo
我正在开发一个 Sticky Notes 项目并在 WPF 中做 UI,显然将 MVVM 作为我的架构设计选择。我正在重新考虑我的模型、 View 和 View 模型应该是什么。 我有一个名为 Not
不要混淆:How can I convert List to Hashtable in C#? 我有一个模型列表,我想将它们组织成一个哈希表,以枚举作为键,模型列表(具有枚举的值)作为值。 publi
我只是花了一些时间阅读这些术语(我不经常使用它们,因为我们没有任何 MVC 应用程序,我通常只说“模型”),但我觉得根据上下文,这些意味着不同的东西: 实体 这很简单,它是数据库中的一行: 2) In
我想知道你们中是否有人知道一些很好的教程来解释大型应用程序的 MVVM。我发现关于 MVVM 的每个教程都只是基础知识解释(如何实现模型、 View 模型和 View ),但我对在应用程序页面之间传递
我想realm.delete() 我的 Realm 中除了一个模型之外的所有模型。有什么办法可以不列出所有这些吗? 也许是一种遍历 Realm 中当前存在的所有类型的方法? 最佳答案 您可以从您的 R
我正在尝试使用 alias 指令模拟一个 Eloquent 模型,如下所示: $transporter = \Mockery::mock('alias:' . Transporter::class)
我正在使用 stargazer 创建我的 plm 汇总表。 library(plm) library(pglm) data("Unions", package = "pglm") anb1 <- pl
我读了几篇与 ASP.NET 分层架构相关的文章和问题,但是读得太多后我有点困惑。 UI 层是在 ASP.NET MVC 中开发的,对于数据访问,我在项目中使用 EF。 我想通过一个例子来描述我的问题
我收到此消息错误: Inceptionv3.mlmodel: unable to read document 我下载了最新版本的 xcode。 9.4 版测试版 (9Q1004a) 最佳答案 您没有
(同样,一个 MVC 验证问题。我知道,我知道......) 我想使用 AutoMapper ( http://automapper.codeplex.com/ ) 来验证我的创建 View 中不在我
需要澄清一件事,现在我正在处理一个流程,其中我有两个 View 模型,一个依赖于另一个 View 模型,为了处理这件事,我尝试在我的基本 Activity 中注入(inject)两个 View 模型,
如果 WPF MVVM 应该没有代码,为什么在使用 ICommand 时,是否需要在 Window.xaml.cs 代码中实例化 DataContext 属性?我已经并排观看并关注了 YouTube
当我第一次听说 ASP.NET MVC 时,我认为这意味着应用程序由三个部分组成:模型、 View 和 Controller 。 然后我读到 NerdDinner并学习了存储库和 View 模型的方法
Platform : ubuntu 16.04 Python version: 3.5.2 mmdnn version : 0.2.5 Source framework with version :
我正在学习本教程:https://www.raywenderlich.com/160728/object-oriented-programming-swift ...并尝试对代码进行一些个人调整,看看
我正试图围绕 AngularJS。我很喜欢它,但一个核心概念似乎在逃避我——模型在哪里? 例如,如果我有一个显示多个交易列表的应用程序。一个列表向服务器查询匹配某些条件的分页事务集,另一个列表使用不同
我在为某个应用程序找出最佳方法时遇到了麻烦。我不太习惯取代旧 TLA(三层架构)的新架构,所以这就是我的来源。 在为我的应用程序(POCO 类,对吧??)设计模型和 DAL 时,我有以下疑问: 我的模
我有两个模型:Person 和 Department。每个人可以在一个部门工作。部门可以由多人管理。我不确定如何在 Django 模型中构建这种关系。 这是我不成功的尝试之一 [models.py]:
我是一名优秀的程序员,十分优秀!