我似乎无法在我的Rails 3应用程序中的控制器之外渲染模板.我所做的谷歌搜索很有帮助,我最终在http://www.swombat.com/rails-rendering-templates-outside-of-a-contro上找到了一些有用的信息.但是,这似乎在Rails 3中被打破了.有没有人有任何想法我如何解决这个方法或者可能知道更好的方法?
我的方法:
def render_erb(template_path, params)
view = ActionView::Base.new(ActionController::Base.view_paths, {})
class << view
include ApplicationHelper
end
view.render(:file => "#{template_path}.html.erb", :locals => params)
end
Run Code Online (Sandbox Code Playgroud)
错误:
ActionView::Template::Error: ActionView::Template::Error
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /Users/mikegerstenblatt/Desktop/bluetrain/lib/test.html.erb:17:in `_lib_test_html_erb__366962844_2173671680_68830'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/template.rb:135:in `send'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/template.rb:135:in `render'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/notifications.rb:54:in `instrument'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/template.rb:127:in `render'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:59:in `_render_template'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `instrument'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `instrument'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:56:in `_render_template'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:26:in `render'
from /Users/mikegerstenblatt/Desktop/bluetrain/app/models/generated_website.rb:45:in `render_erb'
from (irb):2
Run Code Online (Sandbox Code Playgroud) 我需要将一些xml发布到web服务,我正在尝试使用HTTParty.有人可以提供一个关于我如何这样做的例子吗?
以下是我需要发布的XML格式:
<Candidate xmlns="com.mysite/2010/10/10" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<FirstName></FirstName>
<LastName></LastName>
<Email></Email>
<Gender></Gender>
</Candidate>
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的班级:
require 'httparty'
class Webservice
include HTTParty
format :xml
base_uri 'mysite.com'
default_params :authorization => 'xxxxxxx'
def self.add_candidate(first_name,last_name,email,gender)
post('/test.xml', :body => "")
end
end
Run Code Online (Sandbox Code Playgroud)
我不太确定如何充实add_candidate.
任何帮助,将不胜感激.
谢谢.
假设我有一个名为my_template.html.erb的Ruby ERB模板,它包含以下内容:
<div><%= @div_1 %></div>
<div><%= @div_2 %></div>
<div><%= @div_3 %></div>
Run Code Online (Sandbox Code Playgroud)
有没有办法可以编程方式列出模板中的所有可用变量?
例如,以下方法:
def list_out_variables
template = File.open("path_to/my_template.html.erb", "rb").read
erb = ERB.new( template )
erb.this_method_would_list_out_variables
end
Run Code Online (Sandbox Code Playgroud)
会返回类似的东西:
['div1','div2','div3']
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
谢谢,迈克