如何修复'LoadError in Users :: RegistrationsController#create'

时间:2019-08-06 16:57:05

标签: ruby-on-rails

我当前正在使用5.0.7.2上的ruby创建一个博客,并为用户提供Devise gem。现在,我正在尝试在用户注册时在我的Goal_sections表中创建一条记录(在注册控制器的create方法中),但出现此错误:

  

Users :: RegistrationsController#create中的LoadError

     

无法自动加载恒定的GoalSection,预期   /home/ec2-user/environment/blog/app/models/goal_section.rb进行定义   它

我已经检查了我的模型,并且它们对belongs_to和has_many使用正确的复数形式。

预先感谢您的帮助。

/blog/app/controllers/users/registrations_controller.rb:


class Users::RegistrationsController < Devise::RegistrationsController
  # before_action :configure_sign_up_params, only: [:create]
  # before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  def create
    super do |resource|
      @user = resource
      @goal_section = @user.goal_sections.new(default_goal_section_params)
      if @goal_section.save
        flash[:success] = "Default goal section created!"
        redirect_to '/#'
      else
        flash[:success] = "Failed to make default goal section"
        redirect_to '/no'
      end
    end
  end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_up_params
  #   devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
  # end

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
  # end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end
  private
    def default_goal_section_params
      params = ActionController::Perameters.new({
        goal_section: {
          name: "One-off Goals",
          colour: "ffffff",
          width: 100,
          height: 50,
          x_pos: 0,
          y_pos: 0,
          private: 0
        }
      })
      params.require(:goal_section).permit(:name, :colour, :width, :height, :x_pos, :y_pos, :private)
    end
end

/blog/app/models/goal_section.rb:

class Goal_section < ApplicationRecord
  belongs_to :user
  has_many :goals
end

/blog/config/routes.rb:

Rails.application.routes.draw do
  User.connection
  devise_for :users, controllers: {sessions: 'users/sessions', registrations: 'users/registrations'}
  root 'pages#home'
  resources :users do
    resources :goal_sections
  end
  resources :goal_sections do
    resources :goals
  end
end

1 个答案:

答案 0 :(得分:2)

您的模型必须是

class GoalSection < ApplicationRecord

就像在错误Unable to autoload constant GoalSection中一样,现在您正在模型中设置Goal_section