2013-05-01 32 views
2

我刚开始尝试使用生成器。在我生成的视图模板之一,我想打电话给render像这样:Rails:调用发生器内的渲染

index.js.slim 
    transition("#main", "<%= escape_javascript(render 'index') %>"); 

当我尝试使用发电机,我得到这个错误:

(erb):1:in `template': undefined method `render' for #<Slim::Generators::ScaffoldGenerator:0x000000041b2a20> (NoMethodError) 

是Rails的真的不能从调用render的在发电机内?或者我做错了什么?

回答

1

Railscast 218进入更多细节:

The first thing to note is that because we’re using the template method, all of the erb tags in the code will be executed when the generator runs. If we want to include any erb in the generated file we’ll have to escape the percent sign at the beginning of each erb tag and we’ve done that for most of the erb code above.

在这种情况下:

transition("#main", "<%= escape_javascript(render 'index') %>"); 

应该变成:

transition("#main", "<%%= escape_javascript(render 'index') %>"); 

它所需要的是一个额外的%逃脱ERB。