Minitest :: UnexpectedError:ActiveRecord :: RecordNotUnique:Mysql2 :: Error:

时间:2017-01-05 13:35:01

标签: ruby-on-rails ruby

我收到了错误

Minitest::UnexpectedError: ActiveRecord::RecordNotUnique: \
Mysql2::Error: Duplicate entry '' for key 'index_users_on_email': \ 
INSERT INTO `users` (`created_at`, `updated_at`, `id`) \
VALUES ('2017-01-05 13:05:14', '2017-01-05 13:05:14', 298486. 

我在home_controller中写道,

class HomeController < ApplicationController
   before_filter :find_user, only: [:index]

  def index
    @id = params[:id]
    @email = [:email]
    if @id == @user.id  && @email == @user.email
      render :text =>   "sucsess"
    else
      render :text =>   "fail"
    end
  end

  def create
    userData = UserData.new(create_params)
    user = User.find(params[:id]).to_json

    # エラー処理
    unless  userData.save
        @error_message = [memo.errors.full_messages].compact
    end
        end

  private
  def find_user
      @user = User.find(params[:id]) # You should specify this code what are your comparing to.
    end
end

之前,我收到用户变量为零的错误。

所以,我首次亮相

user = User.find(params[:id]).to_json 

我收到了如上所述的错误。

但是在localhost:3000中,我收到了错误

Routing Error undefined method ` ' for HomeController:Class.
因此,我真的无法理解有什么不对。 我该如何解决这个错误?

在routes.rb中

,我写了

Rails.application.routes.draw do
  devise_for :users
  root to: "home#index"
  get 'home/index'


  namespace :home, default: {format: :json} do
    resources :index, only: :create
  end

在test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

home_controller_test.rb中的

require 'test_helper'

class HomeControllerTest < ActionController::TestCase
  test "should get index" do
    get :index
    assert_response :success
  end

end

0 个答案:

没有答案