问题描述
我有一个相当复杂的表单的应用程序,它在lib文件夹中使用Ruby对象(我第一次使用ruby对象)。我不断收到像这样的未初始化的常量错误:
未初始化的常量ProjectsController :: ProjectUpdater
我的表单试图做的是创建初始任务&基于预先制定的模板任务和里程碑的入门级里程碑。所以当用户创建一个项目时,他们已经添加了一些常见的任务。这个代码直接涉及这个前面的问题&
我的控制器: ApplicationController
def new_milestones
@ project.milestones.build
@ project.tasks.build
@milestones_templates = MilestoneTemplate.where(template_id:@project.template_id)
结束
def更新
respond_to do | format |
** result = ProjectUpdater.perform(@project,update_params)== true **< - 错误在这一行
if result == true
format.html {redirect_to }项目已成功更新。}
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]])
结束
结束
/ projec t_updater.rb
class ProjectUpdater
def self.perform(project,params)
milestones = params [:project] [:milestones]
#创建并保存每个里程碑
#您可以使用嵌套属性来保存任务。
如果project.update_attributes(params [:project])
返回true
else
返回项目
结束
结束
结束
我的表格
<%= form_for @project do | f | %GT;
< br>
<%= fm.hidden_field:name,value:milestone.name%>
<%= fm.label milestone.name%>
<%= fm.check_box:milestone_template_id,{},milestone.id%>
< br>
<%= ft.label task.name%>
<%= ft.check_box:task_ids,{},task.id%>
<%end%>
<%end%>
< br>
<%end%>
<%end%>
< br>
<%= f.submit%>
<%end%>
我认为您需要在 / lib
目录,并确保你遵循命名约定..就像
#在lib / foo.rb中:
class Foo
end
$ b $在lib / foo / bar.rb中:$ b $ class Foo :: Bar
结束
#在你的情况下,在lib / project_updater.rb
class ProjectUpdater
def self.perform(your_params ,,,)
#你的代码
end
end
在config / application.rb中:
config.autoload_paths<< %W(#{config.root} / lib)
这可能对你有帮助, / p>
I have an app with a fairly complicated form that uses Ruby Objects in the lib folder (my first time with ruby objects). I keep getting an uninitialized constant error like this: Add Multiple Nested Attributes through checkboxes Rails 4 (maybe with multiple forms)
uninitialized constant ProjectsController::ProjectUpdater
What my form is trying to do is create starter tasks & starter milestones based on premade "template" tasks and milestones. So when a user creates a Project, they have some common tasks already added. This code relates directly to this previous question & answer:
my controller:
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
my form
<%= 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 %>
I think you need to require the ruby files in the /lib
directory and make sure you follow the naming convention.. like
# 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
in config/application.rb:
config.autoload_paths << %W(#{config.root}/lib)
This may help you, prehaps :)
这篇关于在lib文件夹中使用Ruby对象时出现未初始化的常量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!