在lib文件夹中使用Ruby Object时未初始化的常量错误

时间:2015-03-18 12:47:32

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

我有一个相当复杂的应用程序,在lib文件夹中使用Ruby Objects(我第一次使用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 %>

1 个答案:

答案 0 :(得分: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)

这可能会帮助你,或许:)