ActiveModel
是一個讓每個 ORM 實作的類別,允許 Rails 產生客製化的控制器程式碼。
這個 API
擁有與 ActiveRecord
相同的方法,但每個方法都回傳一個符合 ORM API
的字串。
例如
ActiveRecord::Generators::ActiveModel.find(Foo, "params[:id]")
# => "Foo.find(params[:id])"
DataMapper::Generators::ActiveModel.find(Foo, "params[:id]")
# => "Foo.get(params[:id])"
在初始化時,ActiveModel
接受將接收呼叫的實例名稱
builder = ActiveRecord::Generators::ActiveModel.new "@foo"
builder.save # => "@foo.save"
ActiveModel
中,針對 ActiveRecord
的唯一例外是使用 self.build 取代 self.new。
方法
屬性
[R] | name |
類別公開方法
all(klass) 連結
用於
-
GET
index
來源:顯示 | 在 GitHub 上
# File railties/lib/rails/generators/active_model.rb, line 38 def self.all(klass) "#{klass}.all" end
build(klass, params = nil) 連結
用於
-
GET
new
-
POST
create
來源:顯示 | 在 GitHub 上
# File railties/lib/rails/generators/active_model.rb, line 56 def self.build(klass, params = nil) if params "#{klass}.new(#{params})" else "#{klass}.new" end end
find(klass, params = nil) 連結
用於
-
GET
show
-
GET
edit
-
PATCH / PUT
update
-
DELETE
destroy
來源:顯示 | 在 GitHub 上
# File railties/lib/rails/generators/active_model.rb, line 48 def self.find(klass, params = nil) "#{klass}.find(#{params})" end
new(name) 連結
來源:顯示 | 在 GitHub 上
# File railties/lib/rails/generators/active_model.rb, line 31 def initialize(name) @name = name end
實例公開方法
destroy() 連結
用於
-
DELETE
destroy
來源:顯示 | 在 GitHub 上
# File railties/lib/rails/generators/active_model.rb, line 89 def destroy "#{name}.destroy!" end
errors() 連結
用於
-
POST
create
-
PATCH / PUT
update
來源:顯示 | 在 GitHub 上
# File railties/lib/rails/generators/active_model.rb, line 82 def errors "#{name}.errors" end
save() 連結
用於
-
POST
create
來源:顯示 | 在 GitHub 上
# File railties/lib/rails/generators/active_model.rb, line 67 def save "#{name}.save" end
update(params = nil) 連結
用於
-
PATCH / PUT
update
來源:顯示 | 在 GitHub 上
# File railties/lib/rails/generators/active_model.rb, line 74 def update(params = nil) "#{name}.update(#{params})" end