2013-04-17 73 views
0

在我Rails应用程序的阵列我已经有下面的代码:Rails的:加载中谐音

<% %w(number_of_students edit_class_name tech_help).each do |modal| %> 
    <%= render "common/modals/#{modal}" %> 
<% end %> 

会有一些更多的模态加入app/views/common/modals,而是明确地列举出来的%w()我想循环访问common/modals目录并只渲染每个文件。

回答

1

这里是我想出了:

def render_modals 
    files = Dir.glob("#{Rails.root}/app/views/common/modals/*").collect { |file| File.basename(file, ".html.erb").sub("_", "") }.flatten 

    files.collect do |modal| 
     render partial: "common/modals/#{modal}" 
    end.join.html_safe 
    end 
0

定义在哪里比较合适(也许应用助手?)这样一个简单的方法:如果你需要这些模态在

def modals 
    %w(number_of_students edit_class_name tech_help) 
end 

一个控制器/模型,也许你应该在适当的类中定义这个方法?例如

class Modal 
    def self.types 
    %w(number_of_students edit_class_name tech_help) 
    end 
end 

另外,如果您经常渲染模板,然后还定义

def render_modals 
    modals.map do |modal| # Modals here should be the method that you just defined, example, Modal.types 
    render partial: "common/modals/#{modal}" 
    end.join 
end