【问题标题】:How to fix 'LoadError in Users::RegistrationsController#create'如何修复“用户中的加载错误::RegistrationsController#create”
【发布时间】:2019-08-06 16:57:05
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你的模型需要是

    class GoalSection < ApplicationRecord
    

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

    【讨论】:

    • 非常感谢您修复了它
    猜你喜欢
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2014-01-19
    • 2015-02-07
    相关资源
    最近更新 更多