-1

Hello I need to use attr_accessible or something like that.And I am new on Ruby On Rails

That is my post.rb file

    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.

Murat KAYA
  • 23
  • 7

2 Answers2

0

Rails4 uses strong parameters rather than attr_accessibles.

For more info visit doc

Debadatt
  • 5,935
  • 4
  • 27
  • 40
  • I did everything about strong paramters but when I try create post ActiveModel::ForbiddenAttributesError its says forbidden what can I do know – Murat KAYA Mar 26 '14 at 13:27
0

You'll need to use Strong Params for this:

#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
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • ActiveModel::ForbiddenAttributesError in Admin::PostsController#update I'm getting this error stil,its include for create post too... – Murat KAYA Mar 26 '14 at 21:45
  • ` def create @post = Post.new(post_params) respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } format.json { render action: 'show', status: :created, location: @post } else format.html { render action: 'new' } format.json { render json: @post.errors, status: :unprocessable_entity } end end end ` create method – Murat KAYA Mar 26 '14 at 21:56