本文介绍了如何使脚手架生成另一个局部视图模板文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义我的支架生成器,并且我想为同一目录中的视图添加一个新的 partial ,尤其是_item被称为内部索引和显示.我可以获得所有模板,但无法通过

I'm trying to customize my scaffold generator, and I would like to have a new partial for the view in the same directory, in particular _item to be called both inside index and show. I can get all the templates but I'm not able to generate this file through

rails g scaffold foo name:string

我试图将_item.erb与其他文件一起放在/lib/templates/erb/scaffold/中,但是它被忽略了-有人有线索吗?

I tried to put _item.erb in /lib/templates/erb/scaffold/ (together with the other files) but it is ignored- Does anybody have a clue?

我在rails 3上使用ruby,但是请让我知道该解决方案是否也适用于rails 2.我还使用了simple_form(因此我已经有了_form部分),但是我认为该解决方案即使没有也应该是有效的.

I use ruby on rails 3, but please let me know if the solution is valid for rails 2 as well.I also use simple_form (thus I already have the _form partial), but I think the solution should be valid even without it.

推荐答案

刚刚找到它.

它是硬编码的.您可以更改它:

It is hardcoded. You can change it:

https://github.com/rails/rails/blob/master/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb

  def available_views
    %w(index edit show new _form)
  end

在索引模板中,我确实是这样:

In my index template I did like this:

        <thead id="thead_js">
        <%%= render 'thead' %>

        <!-- CUT TO _thead.html.erb -->
        <tr>
            <% for attribute in attributes -%>
                <th><%%= sortable( <%= attribute.name %>, <%= attribute.human_name %> ) %></th>
            <% end %>
            <th>&nbsp;</th>
        </tr>
        <!-- END CUT TO -->

    </thead>

然后,刚刚创建了一个rake任务,该任务读取该注释并创建新文件.

Then, just created a rake task that reads that comment and create the new files.

难看但有效.

这篇关于如何使脚手架生成另一个局部视图模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:56