我正在写一个Rails插件,其中包含一些部分内容.我想测试部分内容,但是我很难设置一个可以渲染它们的测试.没有相关的控制器,所以我只假装一个:
require 'action_controller'
require 'active_support'
require 'action_pack'
require 'action_view'
class MyTest < Test::Unit::TestCase
def setup
@renderer = ActionController::Base.new
@renderer.append_view_path File.expand_path(File.join(File.dirname(__FILE__), '..', 'views'))
end
def test_renders_link
result = @renderer.render(:partial => '/something')
assert ...
end
end
Run Code Online (Sandbox Code Playgroud)
但那个:render电话总是爆炸.我尝试使用的是一个ActionView::Base而不是一个ActionController::Base,但这种情况甚至更少.
有人有成功吗?
我正在尝试让ActionView-Helpercollection_select获取一个将在下拉菜单中预先选择的值.
两者都没有(:selected在html-option-hash中)
<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected => @my_collection_object.id}) %>
Run Code Online (Sandbox Code Playgroud)
也不是(:selected在选项 - 哈希)
<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {:selected => @my_collection_object.id}, {}) %>
Run Code Online (Sandbox Code Playgroud)
似乎工作.
我究竟做错了什么?任何人都可以帮我这个吗?
我有一个关于我当前项目的视图,它执行以下操作(以haml为单位):
-@horses.each do |horse|
= render :partial => 'main/votingbox', :locals => {:horse => horse}
Run Code Online (Sandbox Code Playgroud)
在_votingbox.html.haml文件中,我有以下内容:
%div.votingbox
%span.name= horse.name
%div.genders
- if horse.male
%img{:src => 'images/male.png', :alt => 'Male'}
- if horse.female
%img{:src => 'images/female.png', :alt => 'Female'}
%div.voting_form
= form_for(Vote.new, {:url => horse_vote_path(horse)}) do |f|
= f.label :comment, "Your opinion"
= f.text_field :comment
...
a bunch of labels and input elements follow generated using the form helpers
Run Code Online (Sandbox Code Playgroud)
这将生成工作代码,但它确实为所有表单元素生成具有相同ID的表单,这使得一旦第二次呈现投票箱部分时HTML就无效.
我对修复此问题的第一个猜测是指定一个唯一的:form to form,但它仅适用于form_for生成的表单标记,而不适用于form_for块中的任何标记.
这个问题的一个明确的解决方案是在form_for和我使用的所有表单元素上手动定义我自己的唯一ID.这比我希望的工作更多.
是否有一种更简单或更简洁的方法来获得与Rails当前在我的所有表单元素上生成它们的方式类似的独特ID?
这里有一些代码可以在手机上正常工作:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(IMAGEURI, "image/jpg");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
哪个在Honeycomb(3.0和3.1)上抛出这个:
E/AndroidRuntime(25629): FATAL EXCEPTION: main
E/AndroidRuntime(25629): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://media/external/images/media/282 typ=image/jpg }
Run Code Online (Sandbox Code Playgroud)
真?没有申请在蜂窝上观看jpg?真?有没有人能够在平板电脑上正确地做到这一点?
我在我的视图和邮件中使用以下代码集:
<%= simple_format(auto_link(h(user_input))) %>
Run Code Online (Sandbox Code Playgroud)
我首先在user_input上调用html_safe(h),以逃避任何危险的代码.然后我调用auto_link来启用其输入中的任何链接,然后我调用simple_format来启用换行符等.
这在我的视图中完美运行,并正确显示以下内容,完全转义,但有一个工作链接:
" http://google.com "
Run Code Online (Sandbox Code Playgroud)
但是,当ActionMailer电子邮件中显示完全相同时,我看到所有特殊字符,包括我的自动链接,双重转义(&quot;例如,结果无法正确显示):
&quot; <a href=3D"http://google.com">http://google.=com</a> &quot;
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我需要再次将其重新标记为html_safe以使其正常工作:
<%= simple_format(auto_link(h(user_input))).html_safe %>
Run Code Online (Sandbox Code Playgroud)
这正确输出:
" <a href=3D"http://google.com">http://google.com</a> "
Run Code Online (Sandbox Code Playgroud)
有关为什么ActionView和ActionMailer以不同方式处理相同代码的任何想法?
我有一个ruby gem,poirot,可以在Rails中使用小胡子模板.我的模板处理程序是从ActionView :: Template :: Handler扩展,但是在Rails 3.1中似乎不推荐使用它.
我重新考虑了处理程序以遵守弃用警告.在执行此操作时,我现在无法将本地或视图上下文传递给模板.我似乎无法找到如何使用Rails 3.1.
module Poirot
class Handler
attr_reader :template
def initialize(template)
@template = template
end
def self.call(template, *args)
self.new(template).call
end
def call
view_path = "#{template.virtual_path}_view"
abs_view_path = Rails.root.join('app/views', view_path)
view_class = begin
view_path.classify.constantize
rescue NameError => e
Poirot::View
end
"#{view_class}.new(self, '#{template.source.gsub(/'/, "\\\\'")}').render.html_safe"
end
end
end
Run Code Online (Sandbox Code Playgroud)
在我上面的处理程序代码中,我传递了模板,它是ActionView :: Template的一个实例.但我不知道如何获取视图上下文,其中应包括本地等
有人能指出我正确的方向吗?
我正在使用客户端中的JavascriptMVC和ejs模板开发Rails 3.1应用程序,以便在我的应用程序上执行一些复杂的功能.不幸的是,ejs语法与erb语法非常相似,我无法将代码保存在同一个文件中(尽管如果有人知道这样做的好方法,我会欣喜若狂).最终我希望能够在ejs模板中应用一些rails代码(比如说I18n)但是在这一点上我会满足于让它工作
按照这个问题的例子,我创建了一个自定义模板处理程序,如下所示:
module CommonModel
class Handler < ActionView::Template::Handler
include ActionView::Template::Handlers::Compilable
def compile(template)
template.source.inspect
end
end
end
ActionView::Template.register_template_handler :ejs, CommonModel::Handler
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个包含我的ejs代码的部分模板:_jmvc_templates.html.ejs
<script type="text/ejs" id="my_ejs_template">
<div>Some ejs here</div>
</script>
Run Code Online (Sandbox Code Playgroud)
在我现有的模板中,我尝试包含我的部分:
<%= render 'path/to/my/ejs/templates/jmvc_templates' %>
Run Code Online (Sandbox Code Playgroud)
此时,文件被包含并且我的处理程序被使用,但是所有内容都被转义,因此我的模板中的div会被渲染到页面,如下所示:
<div%gt;
Run Code Online (Sandbox Code Playgroud)
我确定我在这里遗漏了一些明显的东西,但我不知道它可能是什么......我怎样才能让这个模板处理程序只包含我的基于ejs的模板而不转义其中的所有html?
编辑:
我发现用html_safe调用render工作:
<%= render('path/to/my/ejs/templates/jmvc_templates').html_safe %>
Run Code Online (Sandbox Code Playgroud)
这看起来像一个kludge - 必须有一种方法让erb渲染器将我的处理程序中的文本视为html安全文本.
默认情况下,Rails可以在文件名中找到格式,区域设置和模板语言的视图(所以我可以创建index.de.json.erb)
是否可以在视图的文件名中添加另一个自定义参数?
我想传递当前的子域,因此http://foo.example.com/将呈现index.foo.html.erb,http://bar.example.com/并将呈现index.bar.html.erb(两者都具有index.html.erb回退).
我试图以两种方式使用rails_admin路由
第一个工作正常,当点击'用户'链接时,它导航到raisl_admin用户列表页面与rails_admin布局.在第二个不工作的地方,它试图从我的应用程序获取布局,所以我收到错误
Missing partial /rails_admin/user with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :haml, :slim]}
Run Code Online (Sandbox Code Playgroud)
这两个链接有什么区别?如何让第二个工作?
以下异常间歇性地出现:
An ActionView::Template::Error occurred
execution expired
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
Run Code Online (Sandbox Code Playgroud)
这是完整的痕迹:
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
vendor/bundle/ruby/1.9.1/gems/tzinfo-0.3.37/lib/tzinfo/timezone.rb:103:in `get'
vendor/bundle/ruby/1.9.1/gems/tzinfo-0.3.37/lib/tzinfo/timezone_proxy.rb:80:in `real_timezone'
vendor/bundle/ruby/1.9.1/gems/tzinfo-0.3.37/lib/tzinfo/timezone_proxy.rb:52:in `period_for_utc'
vendor/bundle/ruby/1.9.1/gems/tzinfo-0.3.37/lib/tzinfo/timezone.rb:458:in `current_period'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/try.rb:36:in `try'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/values/time_zone.rb:212:in `utc_offset'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/values/time_zone.rb:226:in `<=>'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/values/time_zone.rb:334:in `sort'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/values/time_zone.rb:334:in `all'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/helpers/form_options_helper.rb:507:in `time_zone_options_for_select'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/helpers/form_options_helper.rb:612:in `to_time_zone_select_tag'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/helpers/form_options_helper.rb:277:in `time_zone_select'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/helpers/form_options_helper.rb:654:in `time_zone_select'
Run Code Online (Sandbox Code Playgroud)
当我导航到相关页面时,它加载正常.它似乎对大多数人来说很好,并且只是间歇地(很少)像这样崩溃.我注意到,只要它发生,用户代理就是机器人; 最近的是Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html).但是我担心它有一天会发生在人类身上,所以我想知道是否有人知道我能做些什么呢?
今天就出现了,用户已登录(幸运的是,该用户是我......虽然无法复制它).
execution expired
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/visitor.rb:15:in `visit'
Run Code Online (Sandbox Code Playgroud)
跟踪:
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/visitor.rb:15:in `visit'
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/visitor.rb:5:in `accept'
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/emitter.rb:36:in `block in visit_Psych_Nodes_Sequence'
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/emitter.rb:36:in `each'
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/emitter.rb:36:in `visit_Psych_Nodes_Sequence'
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/visitor.rb:15:in `visit'
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/visitor.rb:5:in `accept'
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/emitter.rb:26:in `block in visit_Psych_Nodes_Document'
vendor/bundle/ruby/1.9.1/gems/psych-1.3.4/lib/psych/visitors/emitter.rb:26:in …Run Code Online (Sandbox Code Playgroud) actionview ×10
ruby ×2
actionmailer ×1
android ×1
ejs ×1
haml ×1
jpeg ×1
mustache ×1
plugins ×1
rails-admin ×1
routes ×1
testing ×1
xss ×1