制作设计是否响应html和json? [英] Making Devise respond to both html and json?

查看:101
本文介绍了制作设计是否响应html和json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,这个应用程序需要有网站和api,我正在实施一个常见的Devise身份验证。为了实现这个要求,我将覆盖Devise注册控制器来响应html和json。对于json,我希望控制器返回用户数据,我可以实现这一点,但是对于html,我想使用原来的devise行为登录并重定向到根路径。



注册控制器的代码是:

  class RegistrationsController< Devise :: RegistrationsController 

def create
@user = User.create(user_params)
respond_to do | format |
format.html {
@ user.save? (超级):(render:new)
}
format.json {
@ user.save? (render:json => {:state => {:code => 0},:data => @user}):
(render:json => {:state => :code => 1,:messages => @ user.errors.full_messages}})
}
end
end

private

def user_params
params.require(:user).permit(:email,:password)
end
end

使用此控制器我得到验证错误电子邮件已被采取,但是当我检查我的日志时,它显示用户已被创建。 p>

任何人都可以告诉我我在做什么错误?

解决方案

问题是当您使用HTML时,我的控制器会回退到原始设计功能,您通过运行 @user = User.create(user_params) super 两次创建了该用户。我将第一行代码移动到JSON格式,而HTML只运行 super

 code> class RegistrationsController< Devise :: RegistrationsController 
def create

respond_to do | format |
format.html {
super
}
format.json {
@user = User.create(user_params)
@ user.save? (render:json => {:state => {:code => 0},:data => @user}):
(render:json => {:state => :code => 1,:messages => @ user.errors.full_messages}})
}
end
end

private

def user_params
params.require(:user).permit(:email,:password)
end
end


I am working on an app which needs has both website and an api and I am implementing a common Devise authentication for that. To implement this requirement I am overriding Devise Registrations Controller to respond to both html and json. For json I want the controller to return the user data and I am able to implement this, but for html I want to use the original devise behavior of signing in and redirecting to root path.

The code for Registrations Controller is:

class RegistrationsController < Devise::RegistrationsController

 def create
  @user = User.create(user_params)
  respond_to do |format|
    format.html {
      @user.save ? (super) : (render :new)
    }
    format.json {
      @user.save ? (render :json => {:state => {:code => 0}, :data => @user }) : 
                   (render :json => {:state => {:code => 1, :messages => @user.errors.full_messages} })
    }
  end
end

private

  def user_params
     params.require(:user).permit(:email, :password)
  end
end 

Using this controller I get a validation error Email has already been taken but when I check my logs it shows the user has been created.

Can anyone tell me what error I am doing? I just want my controller to fallback to original devise functionality in case of html request.

解决方案

The problem was when you used HTML, you created the user twice by running both @user = User.create(user_params) and super. I moved that first line of code to JSON format, and HTML to only run super.

class RegistrationsController < Devise::RegistrationsController
  def create

    respond_to do |format|
      format.html {
        super
      }
      format.json {
        @user = User.create(user_params)
        @user.save ? (render :json => {:state => {:code => 0}, :data => @user }) : 
                     (render :json => {:state => {:code => 1, :messages => @user.errors.full_messages} })
      }
    end
  end

private

    def user_params
      params.require(:user).permit(:email, :password)
    end
  end 

这篇关于制作设计是否响应html和json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆