ROR 5 API - 执行POST操作时出现内部服务器错误

时间:2017-03-12 10:04:49

标签: ruby-on-rails ruby rest api

我试图在很多天前解决这个问题。我真的不知道如何解决这个问题。我只是一个铁杆的初学者,我正在创建一个个人使用的API。这是我的代码:

users_controller.rb:

class UsersController < ApplicationController
  def index
    users = orchestrate_query(User.all)
    render serialize(users)
  end

  def show
    render serialize(user)
  end

  def create
    user = User.new(user_params)
    if user.save
      UserMailer.confirmation_email(user).deliver_now
      render serialize(user).merge(status: :created, location: user)
    else
      unprocessable_entity!(user)
    end
  end

  def update
    user = User.find(params[:id])
    if user.update(user_params)
      render serialize(user).merge(status: :ok)
    else
      unprocessable_entity!(user)
    end
  end

  def destroy
    user.destroy
    render status: :no_content
  end

  private

  def user
    @user ||= params[:id] ? User.find_by!(id: params[:id]) : User.new(user_params)
  end

  alias_method :resource, :user

  def user_params
      params.require(:data).permit(:email, :password, :given_name, :family_name, :role, :confirmation_redirect_url)
  end

end

users_confirmation_controller.rb:

class UserConfirmationsController < ActionController::API


before_action :confirmation_token_not_found

  def show
    user.confirm
    if user.confirmation_redirect_url
      redirect_to(user.confirmation_redirect_url)
    else
      render plain: 'You are now confirmed!'
    end
  end

  private

  def confirmation_token_not_found
    render(status: 404, plain: 'Token not found') unless user
  end

  def confirmation_token
    @confirmation_token ||= params[:confirmation_token]
  end

  def user
    @user ||= User.where(confirmation_token: confirmation_token).first
  end
end

user.rb - &gt;模型

class User < ApplicationRecord
  has_secure_password

  before_validation :generate_confirmation_token, on: :create
  before_validation :downcase_email

  enum role: [:user, :admin]

  validates :email, presence: true,
              uniqueness: true,
              length: { maximum: 255 },
              format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }

  validates :password, presence: true, length: { minimum: 8 }, if: :new_record?
  validates :given_name, length: { maximum: 100 }
  validates :family_name, length: { maximum: 100 }
  validates :confirmation_token, presence: true, uniqueness: { case_sensitive: true }

  def confirm
    update_columns({
      confirmation_token: nil,
      confirmed_at: Time.now
    })
  end

  private

  def generate_confirmation_token
    self.confirmation_token = SecureRandom.hex
  end

  def downcase_email
    email.downcase! if email
  end

end

user_presenter.rb

class UserPresenter < BasePresenter
  FIELDS = [:id, :email, :given_name, :family_name, :role, :last_logged_in_at,
            :confirmed_at, :confirmation_sent_at, :reset_password_sent_at,
            :created_at, :updated_at]

  sort_by *FIELDS
  filter_by *FIELDS
  build_with *[FIELDS.push([:confirmation_token, :reset_password_token,
                           :confirmation_redirect_url,
                           :reset_password_redirect_url])].flatten

end

的routes.rb

Rails.application.routes.draw do
  scope :api do
    resources :resorts, except: :put
    resources :contact_info, except: :put
    resources :users, except: :put

    resources :user_confirmations, only: :show, param: :confirmation_token

    get '/search/:text', to: 'search#index'
  end

  root to: 'resorts#index'
end

这是我对我的REST客户端的请求; 方法:POST,URL:http://localhost:3000/api/users; 标题:application / json;和身体是:

{"data":{"email":"john@gmail.com",
"password": "password",
"confirmation_redirect_url":"http://google.com"}}

这是错误:

"error": "Internal Server Error",
app/controllers/users_controller.rb:12:in `create'

请帮帮我。我现在坚持过去3天了。     app / controllers / users_controller.rb:12:在'create&#39;

2 个答案:

答案 0 :(得分:0)

使用has_secure_password时,需要向users表password_digest:string添加一个属性。我不明白为什么你的错误没有正确显示,但这应该解决问题。

http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html

has_secure_password

简短定义

添加用于设置和验证BCrypt密码的方法。此机制要求您具有password_digest属性。

答案 1 :(得分:-1)

您的用户参数要求您调用用户模型以访问并保存您在用户表中定义的属性

  def user_params
      params.require(:user).permit(:email, :password, :given_name, :family_name, :role, :confirmation_redirect_url)
  end