未定义的方法`report_profile&#39; for#<class:0x00000109953940> </class:0x00000109953940>

时间:2014-05-05 19:23:56

标签: ruby-on-rails ruby-on-rails-4 refactoring

正在重构我填入控制器的一些代码,将其拉出模型......但它不起作用,我无法弄清楚原因。 =(

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

控制器

@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

模型

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

2 个答案:

答案 0 :(得分:2)

您应该将方法定义为class method而不是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

答案 1 :(得分:2)

类方法需要引用self

这应该有效

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