6

确定,所以我在应用控制器中的辅助方法:渲染从是helper_method局部

def run_test(test_name) 
    #computation stuff 
    render :partial => test_name 
end 

我调用它像这样在的观点:

<%= run_test("testpartial") %> 

和它呈现确定只有1(尽管...渲染部分似乎是返回一个数组而不是部分内容?),但是如果我将run_test助手调用放入视图中两次,我会得到一个双重渲染错误,这不应该发生在partial中。

任何想法?

+0

红宝石哪个版本和铁轨你使用,我不运行3.1时得到这种行为? –

+0

3.1。我正在使用RC候选人,但我升级只是为了确保它仍然无法正常工作。 – Msencenb

+0

嗯...所以原来我在application_controller中使用helper_method定义了这个帮助器方法:run_test但是将它移动到helpers文件夹中的application_helper文件中工作。所以..我理解了应用程序控制器中定义的助手和助手文件中定义的助手之间的区别。任何人都可以填补我? – Msencenb

回答

6

render在控制器中与在视图中的render是不同的方法。控制器最终在视图上调用render,但控制器的render方法本身只能调用一次。它看起来像这样:

# Check for double render errors and set the content_type after rendering. 
def render(*args) #:nodoc: 
    raise ::AbstractController::DoubleRenderError if response_body 
    super 
    self.content_type ||= Mime[formats.first].to_s 
    response_body 
end 

请注意如果多次调用它会如何引发?

当你调用helper_method你给视图的代理到控制器的版本render,这是不打算以同样的方式被用作ActionView的,这,不像控制器,预计被称为重复渲染部分和什么。

-2

你可以尝试使用render_to_string方法在视图助手

render_to_string :partial => test_name, :layout => false 
+4

render_to_string在视图助手中不可用。 –

5

貌似对Rails 3.2这只是工作:

# application_helper.rb 
def render_my_partial 
    render "my_partial" 
end 
+1

这也适用于Rails 3.0。这看起来对我来说是最好的答案。 – sockmonk

+1

我不得不在部分名为“_my_partial.html.erb”上使用渲染“my_partial.html.erb”,但这工作。 –

+0

问题是如何在由控制器的'helper_method'暴露的控制器上的方法中呈现部分。这并没有解决这个问题。 – numbers1311407