N
N
nluparev2018-05-09 11:31:38
Ruby on Rails
nluparev, 2018-05-09 11:31:38

Why can't I access a private method from the same object?

class SayController < ApplicationController
  def hello
    @message = { message: 'Hello', payload: self.payload }
    render json: @message
  end

  private
    def payload
      { size: 12, file_type: 'png', name: 'photo!' }
    end
end

If I try to call the payload method, I get an error that I can't call that method. If I remove private then everything is ok.
Why can't I do this, I'm accessing from the same object?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Blokhin, 2018-05-09
@nluparev

replace self.payload with payload.
An object is not allowed to call private methods.
Only a class can do this internally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question