Ruby on Rails参数已发送到Controller,但尚未保存在数据库中

时间:2018-12-27 13:33:02

标签: ruby-on-rails ruby

我从邮递员发送了参数。参数已成功发送到服务器。但是Controller不会将其保存到数据库中。

我尝试在User.rb中创建初始化程序,但不适用于初始化程序“参数丢失。给定1,预期为3”错误消息。

所以我删除了初始化程序,服务器可以很好地接收参数,但是无法保存在数据库中。

enter image description here

User.rb

class User < ApplicationRecord  
  attr_accessor :email, :pwd, :usertype  

=begin  
 def initialize(email, pwd, usertype) @email=email, @pwd=pwd, @usertype=usertype end=end  

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i  
  validates :email, presence: true, uniqueness: { case_sensitive: false},  
  format: {with: VALID_EMAIL_REGEX}, length: {minimum:3, maximum:50}  

  validates :pwd, presence: true, length: {minimum:3, maximum:25}  

  has_many :bookings  


end

users_controller.rb

class UsersController < ApplicationController  
  before_action :set_user, only: [:show, :update, :destroy]  

  # GET /users  
 # GET /users.json  def index  
  @users = User.all  
  end  

  # GET /users/1  
 # GET /users/1.json  def show  
  end  

  # POST /users  
 # POST /users.json  def create  
  @user = User.new(user_params)  

    if @user.save  
      render :show, status: :created, location: @user  
  else  
  render json: @user.errors, status: :unprocessable_entity  
  end  
 end  
  # PATCH/PUT /users/1  
 # PATCH/PUT /users/1.json  def update  
  if @user.update(user_params)  
      render :show, status: :ok, location: @user  
  else  
  render json: @user.errors, status: :unprocessable_entity  
  end  
 end  
  # DELETE /users/1  
 # DELETE /users/1.json  def destroy  
  @user.destroy  
  end  

  private  
  # Use callbacks to share common setup or constraints between actions.  
  def set_user  
  @user = User.find(params[:id])  
    end  

  # Never trust parameters from the scary internet, only allow the white list through.  
  def user_params  
  params.require(:user).permit(:email, :pwd, :usertype)  
      # params.permit!  
  end  
end

routes.rb

Rails.application.routes.draw do  
  resources :bookings  
  resources :users  
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html  
  #  

end

服务器消息

Started POST "/users" for 127.0.0.1 at 2018-12-27 21:53:13 +0900
   (2.6ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
  �넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
   (1.0ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
  �넶 C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by UsersController#create as */*
  Parameters: {"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan", "user"=>{"email"=>"admin2@namver.com", "pwd"=>"Reza Adha", "usertype"=>"Hamonangan"}}
   (0.5ms)  BEGIN
  �넶 app/controllers/users_controller.rb:20
  User Exists (0.6ms)  SELECT  1 AS one FROM `users` WHERE `users`.`email` = 'admin2@namver.com' LIMIT 1
  �넶 app/controllers/users_controller.rb:20
  User Create (2.0ms)  INSERT INTO `users` (`created_at`, `updated_at`) VALUES ('2018-12-27 12:53:13', '2018-12-27 12:53:13')
  �넶 app/controllers/users_controller.rb:20
   (60.1ms)  COMMIT
  �넶 app/controllers/users_controller.rb:20
  Rendering users/show.json.jbuilder
  Rendered users/_user.json.jbuilder (0.8ms)
  Rendered users/show.json.jbuilder (280.3ms)
Completed 201 Created in 1431ms (Views: 1299.2ms | ActiveRecord: 102.5ms)

如您所见,服务器收到的参数很好。但控制器仅插入created_at,updated_at日期列。

enter image description here

1 个答案:

答案 0 :(得分:1)

只需删除此行

attr_accessor :email, :pwd, :usertype

来自您的User类。

attr_accessor生成的setter和getter方法会覆盖由Rails自动创建的getter和setter方法。

并且出于安全考虑,请不要将密码以明文形式存储到数据库中。我建议阅读有关has_secure_password的内容。