Ruby on rails Rails Api返回ActiveRecord::RecordNotFound(找不到用户),除非用户在数据库中?
我有一个rails 6 API(后端)运行Vue和颤振前端 当我尝试从Vue向API传递用户凭据时,出现以下错误:Ruby on rails Rails Api返回ActiveRecord::RecordNotFound(找不到用户),除非用户在数据库中?,ruby-on-rails,vue.js,Ruby On Rails,Vue.js,我有一个rails 6 API(后端)运行Vue和颤振前端 当我尝试从Vue向API传递用户凭据时,出现以下错误: app/controllers/signin_controller.rb:6:in `create' Started POST "/signin" for ::1 at 2019-11-13 02:51:58 -0700 Processing by SigninController#create as HTML Parameters: {"user"=>{"email"=
app/controllers/signin_controller.rb:6:in `create'
Started POST "/signin" for ::1 at 2019-11-13 02:51:58 -0700
Processing by SigninController#create as HTML
Parameters: {"user"=>{"email"=>"xxxxxx@xxxxxx.co", "password"=>"[FILTERED]"}}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" IS NULL LIMIT $1 [["LIMIT", 1]]
↳ app/controllers/signin_controller.rb:5:in `create'
Completed 404 Not Found in 6ms (ActiveRecord: 2.1ms | Allocations: 3220)
ActiveRecord::RecordNotFound (Couldn't find User):
app/controllers/signin_controller.rb:5:in `create'
现在我可以进入我的rails控制台,用户可以使用匹配的电子邮件地址存在
我不太确定如何找到并登录此用户
我使用JWT会话来处理令牌,使用axios来调用api
这是我的登录控制器创建操作
def create
user = User.find_by!(email: params[:email])
if user.authenticate(params[:password])
payload = { user_id: user.id }
session = JWTSessions::Session.new(payload: payload, refresh_by_access_allowed: true)
tokens = session.login
response.set_cookie(JWTSessions.access_cookie,
value: tokens[:access],
httponly: true,
secure: Rails.env.production?)
render json: { csrf: tokens[:csrf] }
else
not_authorized
end
end
以下是我从vue发送的完整方法:
signin () {
let formData = new FormData()
formData.append('user[email]', this.user.email)
formData.append('user[password]', this.user.password)
this.$http.plain.post('/signin', formData, {emulateJSON: true})
.then(response => this.signinSuccessful(response))
.catch(error => this.signinFailed(error))
},
signinSuccessful (response) {
if (!response.data.csrf) {
this.signinFailed(response)
return
}
localStorage.csrf = response.data.csrf
localStorage.signedIn = true
this.$router.replace('/dashboard')
},
signinFailed (error) {
this.error = (error.response && error.response.data && error.response.data.error) || ''
delete localStorage.csrf
delete localStorage.signedIn
},
checkSignedIn () {
if (localStorage.signedIn) {
this.$router.replace('/dashboard')
}
}
}
我们将非常感谢您的帮助!提前谢谢 从请求日志中,我们可以看到进入控制器的参数是:
Parameters: {"user"=>{"email"=>"xxxxxx@xxxxxx.co", "password"=>"[FILTERED]"}}
我们还可以看到对数据库的查询:
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" IS NULL LIMIT $1 [["LIMIT", 1]]
特别是
WHERE "users"."email" IS NULL
因此,您传递到查询中的电子邮件为零。
您应该找到这样的用户
User.find_by!(email: params[:user][:email])
或者最好
def create
user = User.find_by!(email: user_params[:email])
...
end
def user_params
params.require(:user).permit(:email, :password)
end
从请求日志中,我们可以看到进入控制器的参数是:
Parameters: {"user"=>{"email"=>"xxxxxx@xxxxxx.co", "password"=>"[FILTERED]"}}
我们还可以看到对数据库的查询:
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" IS NULL LIMIT $1 [["LIMIT", 1]]
特别是
WHERE "users"."email" IS NULL
因此,您传递到查询中的电子邮件为零。
您应该找到这样的用户
User.find_by!(email: params[:user][:email])
或者最好
def create
user = User.find_by!(email: user_params[:email])
...
end
def user_params
params.require(:user).permit(:email, :password)
end
你能把你的
params
对象粘贴到你的控制器上吗?这可能是关于[:email]
键的名称空间的问题。您可以在控制器中粘贴params
对象的外观吗?这可能是[:email]
键如何命名的问题