React应用正在Rails API上调用ruby。我正在建立注册页面,但卡在了注册保存部分。出错
"status": 500,
"error": "Internal Server Error",
"exception": "#<RuntimeError: Could not find a valid mapping for #<User id: 7, email: \"bearatamer+testfive@gmail.com\", created_at: \"2019-06-19 05:35:58\", updated_at: \"2019-06-19 05:35:58\", fullname: \"test five\", provider: nil, uid: nil, image: nil, phone_number: \"6021233423\", description: nil, pin: nil, phone_verified: nil, stripe_id: nil, merchant_id: nil, unread: 0, access_token: \"JHqM4JiyjvdufcReQdYX\", is_commercial_host: false>>"
发送此JSON
{"fullname": "test five", "phone_number": "6021233423", "email":
"testuser+testfive@gmail.com",
"is_commercial_host": "false", "password": "test1234"
}
此api
http://localhost:3000/api/v1/signup
记录已保存到数据库,但保存后出现错误
这是users_controller.rb
class Api::V1::UsersController < ApplicationController
before_action :authenticate_with_token!, only: [:logout]
def facebook
enter code hereif params[:facebook_access_token]
graph = Koala::Facebook::API.new(params[:facebook_access_token])
user_data = graph.get_object("me?fields=name,email,id,picture")
user = User.find_by(email: user_data['email'])
if user
user.generate_authentication_token
user.save
render json: user, status: :ok
else
user = User.new(
fullname: user_data['fullname'],
email: user_data['email'],
uid: user_data['id'],
provider: 'Facebook',
image: user_data['picture']['data']['url']
)
user.generate_authentication_token
if user.save
render json: user, status: :ok
else
render json: { error: user.errors, is_success: false}, status: 422
end
end
else
render json: { error: "Invalid Facebook Token", is_success: false}, status: :unprocessable_entity
end
end
def logout
user = User.find_by(access_token: params[:access_token])
user.generate_authentication_token
user.save
render json: { is_success: true}, status: :ok
end
def login
user = User.find_by(email: params[:email])
if user && user.valid_password?(params[:password])
session[:user_id] = user.id
render json: user, status: :ok
else
render json: { error: user.errors, is_success: false}, status: 422
end
end
def signup
user = User.find_by(email: params[:email])
if user
render json: { error: "Email already exist, click on forget password", is_success: false}, status: :unprocessable_entity
else
user = User.new(
fullname: params[:fullname],
email: params[:email],
phone_number: params[:phone_number],
password: params[:password],
is_commercial_host: params[:is_commercial_host]
)
user.generate_authentication_token
if user.save
render json: user, status: :ok
else
render json: { success: "Email already exist, click on forget password", is_success: true}, status: :ok
end
end
end
end
这是routes.rb
Rails.application.routes.draw do
namespace :api, defautls: {format: :json} do
namespace :v1 do
get '/logout' => 'users#logout'
get '/login' => 'users#login'
post '/login' => 'users#login'
post '/signup' => 'users#signup'
end
end
end
这是user.rb模型
class User < ApplicationRecord
has_secure_password
before_create :generate_authentication_token
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable,
:confirmable, :omniauthable
validates :fullname, presence: true, length: {maximum:
50}
end
def generate_authentication_token
begin
self.access_token = Devise.friendly_token
end while self.class.exists?(access_token: access_token)
end
该错误发生在users_controller.rb上的注册方法的第64行(此行)上
if user.save
答案 0 :(得分:0)
此错误已解决
devise_for :admins, :class_name => 'User'