【问题标题】:uninitialized constant error when using Ruby Object in lib folder在 lib 文件夹中使用 Ruby 对象时出现未初始化的常量错误
【发布时间】:2015-05-21 06:19:13
【问题描述】:

我有一个形式相当复杂的应用程序,它使用 lib 文件夹中的 Ruby 对象(我第一次使用 ruby​​ 对象)。我不断收到一个未初始化的常量错误,如下所示: Add Multiple Nested Attributes through checkboxes Rails 4 (maybe with multiple forms)

uninitialized constant ProjectsController::ProjectUpdater

我的表单试图做的是基于预制的“模板”任务和里程碑创建启动任务和启动里程碑。因此,当用户创建项目时,他们已经添加了一些常见任务。此代码直接与上一个问题和答案相关:

我的控制器:

class ProjectsController < ApplicationController

def new_milestones
    @project.milestones.build
    @project.tasks.build
    @milestones_templates = MilestoneTemplate.where(template_id: @project.template_id)
  end

 def update
    respond_to do |format|
      **result = ProjectUpdater.perform(@project, update_params) == true** <-- the error is on this line
      if result == true
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
      else
        @project = result
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  def project_params
      params.require(:project).permit(:id, :name, :template_id, milestone_attributes:[{:names => []},  {:ids => []}, { :milestone_ids => []},:id, :name, :project_id, :milestone_template_id, :project_id, task_attributes: [{:names => []},  {:ids => []}, { :task_ids => []}, :id, :name, :milestone_id, :task_template_id, :project_id, :_destroy]])
    end
  end

lib/project_updater.rb

class ProjectUpdater

  def self.perform(project, params)
    milestones = params[:project][:milestones]
    #Create and save each milestone
    # You might be able to us nested attributes to save tasks.

    if project.update_attributes(params[:project])
      return true
    else
      return project
    end
  end
end

我的表格

<%= form_for @project do |f| %>
  <% @milestones_templates.each_with_index do |milestone, index| %>
    <br>
    <%= f.fields_for :milestones, index: index do |fm| %>
      <%= fm.hidden_field :name, value: milestone.name %>
      <!-- Create a checkbox to add the milestone_id to the project -->
      <%= fm.label milestone.name %>
      <%= fm.check_box :milestone_template_id,{}, milestone.id %>
      <br>
      <% milestone.task_templates.each_with_index do |task, another_index| %>
        <%= fm.fields_for :tasks, index: another_index do |ft| %>
          <!-- Create a checkbox for each task in the milestone -->
          <%= ft.label task.name %>
          <%= ft.check_box :task_ids, {}, task.id %>
        <% end %>
      <% end %>
      <br>
    <% end %>
  <% end %>
  <br>
<%= f.submit %>
  <% end %>

【问题讨论】:

  • 在这段代码中有ProjectUpdater,它在/lib 中定义。但是,在某个地方,这些是在您的项目控制器中调用ProjectMilestoneBuilder。查找并替换为ProjectUpdater
  • 抱歉,我用你给我的两个版本打开了两个窗口。正确的错误找不到 ProjectUpdater,而不是 ProjectMilestoneBuilder(您提供的旧代码)。我已经纠正了我的问题。此错误来自您提供的代码的纯粹提取。很抱歉造成混乱。我的时间早上 5 点。

标签: ruby-on-rails forms object ruby-on-rails-4


【解决方案1】:

我认为您需要在 /lib 目录中使用 ruby​​ 文件,并确保您遵循命名约定......就像

        # in lib/foo.rb:
        class Foo
        end

        # in lib/foo/bar.rb:
        class Foo::Bar
        end

    # in you case, in lib/project_updater.rb
    class ProjectUpdater
      def self.perform (your_params,,,)
        # your code
      end
    end

在 config/application.rb 中:

config.autoload_paths << %W(#{config.root}/lib)

这可能会对你有所帮助,也许:)

【讨论】:

  • 我对此很陌生....所以我把类 ProjectUpdater 放了两次?一次在 lib/ProjectUpdater.rb 和一次在 lib/ProjectUpdater/ProjectUpdater.rb?同一个文件?
  • 如果你将ProjectUpdater直接放在lib文件夹中,那么它就是第一个例子。但是,如果您将 ProjectUpdater 放在 /lib/something 中,则必须在其周围放置 Something 模块。在这种令人困惑的情况下foo
  • 您可以删除lib/ProjectUpdater/ProjectUpdater.rb并将文件lib/ProjectUpdater.rb重命名为lib/project_updater.rb
  • 至于答案,我建议您在两种情况下都将文件名更改为相同,即。 class Bar::Foo 并通过删除速记 :: 并使用模块调用 module Bar class Foo end end 使命名更加明确和易于理解
  • @Cba Bhusal 我问了另一个相关问题。添加 config.autoload_paths 修复了它。 stackoverflow.com/questions/29131850/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多