render_to_string没有按预期工作

时间:2014-03-19 19:35:18

标签: ruby-on-rails

我正在尝试在我的控制台中使用render_to_string的默认属性..

StoresController.new.render_to_string '/shared/colors'

返回:

ActionView::MissingTemplate: Missing template /shared/colors with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :srt], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
  * "/Sites/whisper.me/app/views"
  * "/.rvm/gems/ruby-1.9.3-p125@project/gems/teaspoon-0.7.7/app/views"
  * "/.rvm/gems/ruby-1.9.3-p125@project/gems/devise-2.2.2/app/views"
  * "/Sites/project"
  * "/"

即使我的部分显然存在。事实上,如果我用任何控制器搜索任何目录中的任何文件,它也无法按预期工作。我错过了什么?

2 个答案:

答案 0 :(得分:1)

使用此

StoresController.new.render_to_string partial: '/shared/colors'

当您尝试渲染部分时,请提供partial选项。

答案 1 :(得分:0)

您确定'/shared/colors'应该有效吗?在Rails Guide之后,我会尝试以下方式:

StoresController.new.render_to_string 'shared/colors'

前导斜杠似乎决定了文件系统路径:

  

渲染方法也可以使用完全在应用程序之外的视图[...]。由于前导斜杠字符,Rails确定这是一个文件渲染。

相关问题