简体   繁体   English

Rails:未定义的方法“ model_name”?

[英]Rails: undefined method `model_name'?

Getting a strange error when trying to output some data as a response to a json request. 尝试输出一些数据作为对json请求的响应时遇到奇怪的错误。 For the life of me I cannot figure out the problem here. 为了我的一生,我无法在这里解决问题。 I am doing something very similar in another controller and not getting any error at all. 我正在另一个控制器中做非常相似的事情,根本没有任何错误。 Any ideas what would be causing this? 有什么想法会导致这种情况吗?

  class SessionsController
  ...
  user = User.authenticate(params[:session][:email].downcase,
                           params[:session][:password].downcase)
  if user.nil?
    respond_with(@error = "Invalid email/password combination.")
  else
    sign_in user
    respond_with([user, user.authenticated_with])
  end

  -------------------------------------

  User.rb
  ...
  def authenticated_with
    fb_hash = {:facebook => (!self.authentications.where("provider = ?", "facebook").empty? ? true : false)}
    tw_hash = {:twitter => (!self.authentications.where("provider = ?", "twitter").empty? ? true : false)}
    providers = [fb_hash, tw_hash]
    return providers
  end

  -------------------------------------

  LOG OUTPUT

  2011-09-05T15:25:51+00:00 app[web.1]: NoMethodError (undefined method `model_name' for Array:Class):
  2011-09-05T15:25:51+00:00 app[web.1]:   app/controllers/sessions_controller.rb:24:in `create'

The log error is referencing this line: 日志错误引用此行:

  respond_with([user, user.authenticated_with])

Any ideas??? 有任何想法吗???

Thanks! 谢谢!

My solution for anyone who finds this. 找到任何人的我的解决方案。 I ended up rewriting 我最终重写了

respond_with()

as a respond_to block: 作为response_to块:

respond_to do |format|
  format.json { render :json => [user, user.authenticated_with]}
end

This solved the problem for me! 这为我解决了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM