-1

I am createing simple ruby blog app I am updating a post but it's showing thi type error error

        def edit
            @post = Post.find(params[:id])
        end

        def update
            @post = Post.find(params[:id])
            if @post
                puts "******************dd*********************"
                puts params[:post]
                puts "**************************************"
                @post.update_attributes(params[:post])


                redirect_to post_path, :notice => "you post  has been updated"
            else
                render "edit"
            end
        end

but when I using this

        @post.update_attributes({"title"=>"2nd", "body"=>"this is second post body", "category_id"=>"2"})

it is work

x-xx-x
  • 3
  • 1
  • 3

3 Answers3

0

You should use strong parameters, Rails prevent you from assigning unfiltered params coming from outside world for security reasons.

def update
  # ...
  @post.update_attributes(post_params)
  # ...
end

# ...

private

def post_params
  params.require(:post).permit(:title, :body, :category_id)
end
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
0

Attributes should be whitelisted for updating.

Below is the code:

        def edit
            @post = Post.find(params[:id])
        end

        def update
           @post = Post.find(params[:id])
           if @post
              @post.update_attributes(post_params)
              redirect_to post_path, :notice => "you post  has been updated"
           else
              render "edit"
           end
        end

   private

    def post_params
      params.require(:post).permit(:title, :body, :category_id)
    end
Beena Shetty
  • 3,676
  • 2
  • 28
  • 31
0

ActiveModel::ForbiddenAttributesError is only raised if you pass unsanitized objects to the record update call.

@post.update_attributes(params[:post]) # This would be an issue

Post.where(params[:post]) # This would not

@post.update_attributes(post_params) # Neither would this

def post_params
  params.require(:post).permit(:title, :body, :category_id)
end
ray
  • 5,454
  • 1
  • 18
  • 40