简体   繁体   English

用于 Rails 4 的 attr_accessible

[英]attr_accessible for Rails 4

Hello I need to use attr_accessible or something like that.And I am new on Ruby On Rails你好,我需要使用 attr_accessible 或类似的东西。我是 Ruby On Rails 的新手

That is my post.rb file那是我的post.rb文件

    class Post < ActiveRecord::Base
  has_many :comments

  attr_accessible :body, :title, :published, :author, :author_id
  belongs_to :author, :class_name => "AdminUser"


  validates_presence_of :body,:title
  scope :published, where(:published => true)

  def content
    MarkdownService.new.render(body)
  end

  def author_name
    if author
      author.name
    else
      "Nobody"
    end
  end


end

what can I do for attr_accesible thanks for your answers.我能为 attr_accesible 做些什么感谢您的回答。

Rails4 uses strong parameters rather than attr_accessibles. Rails4 使用强参数而不是 attr_accessibles。

For more info visit doc有关更多信息,请访问文档

You'll need to use Strong Params for this:为此,您需要使用Strong Params

#app/models/post.rb
class Post < ActiveRecord::Base
  has_many :comments
  belongs_to :author, :class_name => "AdminUser"

  validates_presence_of :body,:title
  scope :published, where(:published => true)

  def content
    MarkdownService.new.render(body)
  end

  def author_name
    if author
      author.name
    else
      "Nobody"
    end
  end


end

#app/controllers/posts_controller.rb
def new
    @post = Post.new
end

def create
    @post = Post.new(post_params)
end

private

def post_params
    params.require(:post).permit(:body, :title, :published, :author, :author_id)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM