attr_accessible
を使用して、モデルのどのフィールドを公開するかを指定すると、script/consoleも同様ですか?私はattr_accessible
がコンソールからもアクセスできないと指定しなかったことを意味しますか?attr_accessible in railsアクティブレコード
答えて
これは、質量割り当てにのみ該当します。たとえば、あなたがいた場合、あなたのモデルでattr_protected :protected
を設定します
>> Person.new(:protected => "test")
=> #<Person protected: nil>
逆に、あなたはすべてのあなたがattr_accessible
を使用してアクセスできるようにしたい属性を設定することができます。
ただし、以下はまだ動作します:
>> person = Person.new
=> #<Person protected: nil>
>> person.protected = "test"
=> #<Person protected: "test">
これは、コントローラと同じ動作である、ビューなどattr_protected
は
なぜ実測値:
は、new(attributes)
、update_attributes(attributes)
、又はattributes=(attributes)
ような質量割り当てを介して設定することができるモデル属性のホワイトリストを指定します。 これはattr_protectedマクロの反対です:
Mass-assignment will only set attributes in this list, to assign to the rest of
attributes you can use direct writer methods. This is meant to protect sensitive
attributes from being overwritten by malicious users tampering with URLs or forms.
If you‘d rather start from an all-open default and restrict attributes as needed,
have a look at `attr_protected`.
だから、それだけで大量の割り当てを避けることを意味しますが、私はまだ値を設定することができます。
コンソールはRailsアプリケーションとまったく同じように動作します。特定のモデルの一部の属性を保護した場合、これらの属性をコンソールまたはRailsアプリケーション自体から一括して割り当てることはできません。
somethingsをattr_accessible
に指定すると、コンソールまたはWebサイトのインターフェイスでこれらのものにアクセスできます。
例:
attr_accessible :name, :email
とcreated_at
とupdated_at
を残し(あなたがすることになっているもの):あなたがattr_accessible
するname
とemail
を作ったとします。 コンソールでこれらのフィールドの編集/更新のみが可能です。
あなたがフィールドを公開したいあなたのモデルを形成した場合、あなたは
attr_accessor :meth # for getter and setters
attr_writer :meth # for setters
attr_reader :meth # for getters
か、あなたの属性にいくつかの動作を追加したい場合は、あなたが仮想属性
def meth=(args)
...
end
def meth
...
end
を使用する必要がありますでしょうを使用することができます乾杯。
- 1. Rails attr_accessible:オブジェクトvs:object_id
- 2. Rails - attr_accessible&mass assignment
- 3. Rails Dev Attr_accessible problem
- 4. データベースアゴニストまたはQuery in Rails 4.2アクティブレコード
- 5. Concord in Codeigniterアクティブレコード
- 6. アクティブレコードの "NOT IN"
- 7. Rails 3 - スプラットattr_accessible with Active Record
- 8. アクティブレコード。 Railsの
- 9. Rails 3 Plugin - アクティブレコードtable_name_prefix
- 10. Railsのアクティブレコード:参照
- 11. attr_accessible/protectedのRails 3設定の設定
- 12. Railsのアクティブレコードの「」メソッド異常
- 13. アクティブレコードをスキップするRails Generator
- 14. レール内の動的attr_accessible
- 15. アクティブレコード - 複数の列を持つIN
- 16. レールattr_accessible
- 17. in in Railsの
- 18. Railsアクティブレコード、属性ごとの制限
- 19. Ruby on Railsのロジックがアクティブレコード状態
- 20. Railsの営業時間アクティブレコードのクエリ
- 21. Rails - アクティブレコードの関係をCSVに渡す
- 22. RailsアクティブレコードOR句と祖父母プロパティ
- 23. アクティブレコード(Railsの)MySQLのセキュア接続今
- 24. Railsの質量割り当て定義とattr_accessible use
- 25. Rails:ジェネレータでattr_accessibleの作成を自動化しますか?
- 26. jQuery in Rails 3 "Beginning Rails 3"
- 27. アソシエーションin Rails 4
- 28. メッセージングシステムin Rails
- 29. Rails Devise:Sign Inカスタマイズ
- 30. Helpers in Railsエンジン