正在重构我填入控制器的一些代码,将其拉出模型......但它不起作用,我无法弄清楚原因。 =(
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
答案 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