문제

Was refactoring some code I had stuffed into the controller, pulling it out into the model...but it isn't working, I can't quite figure out why. = (

undefined method `report_profile' for #<Class:0x00000109953940>

controller

@logged_in_profile = logged_in_profile
@reason = params[:reason]
reported_by = @logged_in_profile
reported = @profile
if(Reported.report_profile(reported_by, reported, @reason))
  flash[:success] = 'Reported profile because ' + @reason
else
  flash[:danger] = 'Length of report was too short. Should be at least 6 characters.'+
    ' Remember we need a reason before we can do anything!'
end
  redirect_to p_path(@profile.nickname)
end

model

  def report_profile(reported_by, reported, reason)
    report = Reported.new
    report.reported = reported
    report.reported_by = reported_by
    report.reason = reason
    report.save
  end
도움이 되었습니까?

해결책

Class methods needs to referenced with self

This should work

def self.report_profile(reported_by, reported, reason)
    report = Reported.new
    report.reported = reported
    report.reported_by = reported_by
    report.reason = reason
    report.save
  end

다른 팁

You should define the method as a class method instead of an instance method:

def self.report_profile(reported_by, reported, reason)
  report = Reported.new
  report.reported = reported
  report.reported_by = reported_by
  report.reason = reason
  report.save
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top