我使用的是Rails 3.0.9。我正在尝试使用液体而且我收到了错误。
这是我的Gemfile
source 'http://rubygems.org'
gem 'rails', '3.0.9'
gem 'sqlite3'
gem 'liquid'
这是一个初始化程序
class LiquidView
PROTECTED_ASSIGNS = %w( template_root response _session template_class action_name request_origin session template
_response url _request _cookies variables_added _flash params _headers request cookies
ignore_missing_templates flash _params logger before_filter_chain_aborted headers )
PROTECTED_INSTANCE_VARIABLES = %w( @_request @controller @_first_render @_memoized__pick_template @view_paths
@helpers @assigns_added @template @_render_stack @template_format @assigns )
def self.call(template)
"LiquidView.new(self).render(template, local_assigns)"
end
def initialize(view)
@view = view
end
def render(template, local_assigns = nil)
@view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'
# Rails 2.2 Template has source, but not locals
if template.respond_to?(:source) && !template.respond_to?(:locals)
assigns = (@view.instance_variables - PROTECTED_INSTANCE_VARIABLES).inject({}) do |hash, ivar|
hash[ivar[1..-1]] = @view.instance_variable_get(ivar)
hash
end
else
assigns = @view.assigns.reject{ |k,v| PROTECTED_ASSIGNS.include?(k) }
end
source = template.respond_to?(:source) ? template.source : template
local_assigns = (template.respond_to?(:locals) ? template.locals : local_assigns) || {}
if content_for_layout = @view.instance_variable_get("@content_for_layout")
assigns['content_for_layout'] = content_for_layout
end
assigns.merge!(local_assigns.stringify_keys)
liquid = Liquid::Template.parse(source)
liquid.render(assigns, :filters => [@view.controller.master_helper_module], :registers => {:action_view => @view, :controller => @view.controller})
end
def compilable?
false
end
end
::ActionView::Template.register_template_handler(:liquid, LiquidView)
这是app / views / users / index.html.liquid
<h1>Listing users</h1>
<table>
<tr>
<th>Name</th>
</tr>
{% for user in @users %}
<tr> <td> {{ user.name }} </td> </tr>
{% endfor %}
</table>
这是控制器
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
end
end
end
我收到此错误。
undefined method `template' for #<UsersController:0x00000100979b80>
Extracted source (around line #1):
1: <h1>Listing users</h1>
2:
3: <table>
4: <tr>
答案 0 :(得分:2)
我认为这应该可以帮助您,只需使用以下代码更改初始化程序
class LiquidView
def self.call(template)
"LiquidView.new(self).render(#{template.source.inspect}, local_assigns)"
end
def initialize(view)
@view = view
end
def render(template, local_assigns = {})
@view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'
assigns = @view.assigns
if @view.content_for?(:layout)
assigns["content_for_layout"] = @view.content_for(:layout)
end
assigns.merge!(local_assigns.stringify_keys)
controller = @view.controller
filters = if controller.respond_to?(:liquid_filters, true)
controller.send(:liquid_filters)
elsif controller.respond_to?(:master_helper_module)
[controller.master_helper_module]
else
[controller._helpers]
end
liquid = Liquid::Template.parse(template)
liquid.render(assigns, :filters => filters, :registers => {:action_view => @view, :controller => @view.controller})
end
def compilable?
false
end
end
ActionView::Template.register_template_handler :liquid, LiquidView
请不要忘记将liquid_methods [,:column_name]放入模型