Rails Grape + jbuilder 做 API 的时候,jbuilder 模板不能解析 hash

tyaccp_guojian · 2013年12月10日 · 最后由 luikore 回复于 2013年12月17日 · 2802 次阅读

想做一个验证的 API,需要返回一个 status,里面包含验证结果和消息

这是 api.rb 文件

post 'sign_in', jbuilder: 'sign_in' do
  if params[:email].present? and params[:password].present?
    @user = User.where("email = ?", params[:email]).first
    if @user.present? and @user.authenticate(params[:password])
      @status = { ok: true }
    else
      @status = { ok: false }
    end
  else
    @status = { ok: false, message: 'Email and password is required'}
  end
end

这时模板文件

json.user do
  if @user.present?
    json.extract! @user, :id, :email
  end
end

json.meta do
  json.ok @status[:ok]
  json.message @status[:message]
end

报错如下

这是为什么?

@status 改个名字吧,估计和 http 返回代码用的同一个实例变量

需要 登录 后方可回复, 如果你还没有账号请 注册新账号