Rails 4.2
Rails 4.2
I've created a multi-step wizard for a User
model i.e. a visitor to the app registers as a User
over a 4 step form. Its working in it's current setup.
我为User模型创建了一个多步骤向导,即应用程序的访问者通过4步表单注册为用户。它正在进行当前的设置。
However, I've had to use require
statements to include several of the wizard related ruby files. As a consequence these files are not auto loaded by Rails.
但是,我必须使用require语句来包含几个与向导相关的ruby文件。因此,Rails不会自动加载这些文件。
I'd like to refactor the relevant files so that they follow Rails conventions and are able to be auto loaded.
我想重构相关文件,以便它们遵循Rails约定并能够自动加载。
Current Structure - is working
当前结构 - 正在运作
app/wizards/user.rb User wizard model - runs validations on each step etc
app / wizards / user.rb用户向导模型 - 对每个步骤运行验证等
module Wizard
module User
STEPS = %w[step1 step2 step3 step4].freeze
# omitted class implementations, not relevant
class Base
end
class Step1 < Base
end
class Step2 < Step1
end
class Step3 < Step2
end
class Step4 < Step3
end
end
end
app/controllers/user_wizards_controller.rb
应用程序/控制器/ user_wizards_controller.rb
# I have to require the file above, would like to avoid
require Rails.root.join('app', 'wizards', 'user.rb')
class UserWizardsController < ApplicationController
# I have to specify the template, would like to avoid
def step1
render 'wizards/users/step1'
end
# Notice how I have to refer to module/classes above.
def wizard_user_for_step(step)
raise InvalidStep unless step.in?(::Wizard::User::STEPS)
"Wizard::User::#{step.camelize}".constantize.new(session[:user_attributes])
end
end
app/views/wizards/users/step1.html.erb
应用程序/视图/精灵/用户/ step1.html.erb
app/views/wizards/users/step2.html.erb
应用程序/视图/精灵/用户/ step2.html.erb
Attempted Solution
试图解决方案
Based on this statement by xfn
基于xfn的这个陈述
The directories in autoload_paths are considered to be root directories, they do not reflect namespacing. For example, the classes below app/models are not under a Models namespace, any namespace has to go within that directory.
autoload_paths中的目录被视为根目录,它们不反映命名空间。例如,app / models下面的类不在Models命名空间下,任何命名空间都必须在该目录中。
The file app/services/doctor_finder.rb for example does not follow autoloading conventions because of that, since it defines Services::DoctorFinder rather than DoctorFinder. Therefore, that file cannot be autoloaded.
例如,文件app / services / doctor_finder.rb不遵循自动加载约定,因为它定义了Services :: DoctorFinder而不是DoctorFinder。因此,该文件无法自动加载。
I'm going for...
我要去......
Models
楷模
app/wizards/user/user.rb
应用程序/向导/用户/ user.rb
app/wizards/user/base.rb
应用程序/向导/用户/ base.rb
app/wizards/user/step1.rb
应用程序/向导/用户/ step1.rb
app/wizards/user/step2.rb
应用程序/向导/用户/ step2.rb
app/wizards/user/step3.rb
应用程序/向导/用户/ step3.rb
app/wizards/user/step4.rb
应用程序/向导/用户/ step4.rb
However, I'm not getting very far. Any ideas?
但是,我没有走得太远。有任何想法吗?
1 个解决方案
#1
1
If you want to autoload these files move it into e.g services or steps directories like:
如果您想自动加载这些文件,请将其移至例如服务或步骤目录中:
app/services/wizards/user/step1
and rails should autoload module:
和rails应该自动加载模块:
module Wizards::User
class Step1
end
end
Depend on rails version you will need to add 'services' to autoload path.
根据rails版本,您需要将“服务”添加到自动加载路径。
Regards views:
问候观点:
render 'wizards/users/step1'
isn't bad and In my opinion could be consider as good practice.(using render method allow you to pass non global variables to view)
不错,在我看来可以认为是一种很好的做法。(使用render方法可以让你传递非全局变量来查看)
If you want to remove this line you should put views for UserWizardsController
to user_wizards/step1.html.xxx
如果要删除此行,则应将UserWizardsController的视图放入user_wizards / step1.html.xxx
or if you want to have view in wizards/users/step1.html.xxx you should scope your controller in that way:
或者如果你想在向导/ users / step1.html.xxx中查看,你应该以这种方式控制你的控制器:
module Wizards
class UsersController < ApplicationController
end
edn
#1
1
If you want to autoload these files move it into e.g services or steps directories like:
如果您想自动加载这些文件,请将其移至例如服务或步骤目录中:
app/services/wizards/user/step1
and rails should autoload module:
和rails应该自动加载模块:
module Wizards::User
class Step1
end
end
Depend on rails version you will need to add 'services' to autoload path.
根据rails版本,您需要将“服务”添加到自动加载路径。
Regards views:
问候观点:
render 'wizards/users/step1'
isn't bad and In my opinion could be consider as good practice.(using render method allow you to pass non global variables to view)
不错,在我看来可以认为是一种很好的做法。(使用render方法可以让你传递非全局变量来查看)
If you want to remove this line you should put views for UserWizardsController
to user_wizards/step1.html.xxx
如果要删除此行,则应将UserWizardsController的视图放入user_wizards / step1.html.xxx
or if you want to have view in wizards/users/step1.html.xxx you should scope your controller in that way:
或者如果你想在向导/ users / step1.html.xxx中查看,你应该以这种方式控制你的控制器:
module Wizards
class UsersController < ApplicationController
end
edn