我想要做的就是规定视图的一行帮助方法应该如何表现,但我不确定如果我在Rails中工作,我应该创建什么样的模拟对象(如果有的话).
这是events_helper.rb的代码:
module EventsHelper
def filter_check_button_path
params[:filter].blank? ? '/images/buttons/bt_search_for_events.gif' : '/images/buttons/bt_refine_this_search.gif'
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的规范代码,在events_helper_spec.rb中:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe EventsHelper do
#Delete this example and add some real ones or delete this file
it "should be included in the object returned by #helper" do
included_modules = (class << helper; self; end).send :included_modules
included_modules.should include(EventsHelper)
end
it "should return the 'refine image search' button if a search has been run" do
# mock up params hash
params = {}
params[:filter] = …Run Code Online (Sandbox Code Playgroud) 在我目前的Rails(Rails 2.3.5,Ruby 1.8.7)应用程序中,如果我希望能够定义一个帮助器,如:
def product_image_tag(product, size=nil)
html = ''
pi = product.product_images.first.filename
pi = "products/#{pi}"
pa = product.product_images.first.alt_text
if pi.nil? || pi.empty?
html = image_tag("http://placehold.it/200x200", :size => "200x200")
else
html = image_tag(pi, size)
end
html
end
Run Code Online (Sandbox Code Playgroud)
...然后从一个视图中调用它:
<%= product_image_tag(p) %>
Run Code Online (Sandbox Code Playgroud)
...要么:
<%= product_image_tag(p, :size => 20x20) %>
Run Code Online (Sandbox Code Playgroud)
换句话说,我希望能够让这个辅助方法采用可选的大小参数.最好的方法是什么?
在Ruby on Rails应用程序中,我有一个控制器,我想要一些有条件运行的功能,其中条件取决于运行应用程序的环境.作为一个人为的例子,在开发模式中我喜欢它做:
if foo == 5:
...
end
Run Code Online (Sandbox Code Playgroud)
在生产模式中,我想:
if foo > 6:
...
end
Run Code Online (Sandbox Code Playgroud)
两个条件之间的差异比单个常数(上例中的5或6)更复杂.
在Rails中最常用的方法是什么?我会直接在environments/文件中编写辅助方法吗?或者向应用程序控制器添加一个检查当前环境的方法?或者是其他东西?
ruby environment ruby-on-rails helpers conditional-statements
我有一个具有很多属性的模型,并构建了一系列页面来收集所有相关数据.在最后一页中,我想向用户显示所有收集的数据.
我可以通过手动键入每个属性的所有标签和值来创建此页面,但我希望这种繁琐和重复的任务已经由某人解决,以便在3-4行代码中.
在这个阶段,我只是原型设计,所以这不需要看起来很好.
任何人都有关于如何在屏幕上快速打印模型的所有属性的任何建议?
我在想这样的事情:
如果@my_data_model是我想要打印属性的实例变量,那么:
<%= show_attributes @my_data_model %>
Run Code Online (Sandbox Code Playgroud)
将使用其标签输出属性值.
谢谢你的期待.
我有一个在application_helper.rb中定义的方法,它根据当前请求返回一个规范的URL.如何模拟或以其他方式指定控制器的完整URL?
# spec/helpers/application_helper_spec.rb
describe "#canonical_url" do
it "should return a path to an asset that includes the asset_host" do
# Given: "http://www.foo.com:80/asdf.asdf?asdf=asdf"
helper.canonical_url().should eq("http://www.foo.com/asdf.asdf")
end
end
# app/helpers/application_helper.rb
def canonical_url
"#{request.protocol}#{request.host}#{(request.port == 80) ? "" : request.port_string}#{request.path}"
end
Run Code Online (Sandbox Code Playgroud)
编辑:
最后,我想测试canonical_url()为一堆不同的URL返回正确的字符串,一些带有端口,一些带有w/o,一些带有查询字符串,一些带有路径等等.也许这太过分了,但这是最终目标.我想明确存根/模拟/无论初始URL,然后在匹配器中显式设置期望.我希望能够在一次通话中做到这一点,即controller.request.url = 'http://www.foo.com:80/asdf.asdf?asdf=asdf'或者request = ActionController::TestRequest.new :url => 'http://www.foo.com:80/asdf.asdf?asdf=asdf'到目前为止我还没有找到一个允许我这样做的"钩子".这就是我正在寻找的解决方案.如何显式定义给定测试的请求URL.
我得到以下链接来解决与类和实例方法相关的问题. http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/
module NotificationsHelper
extend ActiveSupport::Concern
module ClassMethods
def my_class_method
# ...
end
end
module InstanceMethods
def my_instance_method
# ...
end
end
end
但我得到了未初始化的常量ActiveSupport :: Concern错误.我正在使用Rails 2.3.5和Ruby 1.9.2p180
我想知道如何为我的rails助手获得一些动态能力:
<h3><%= link_to object.name, ("#{object.class_path.to_s}")_path(object) %></h3>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,将对象的类扔到链接中.我对如何在辅助方法中抛出方法感到困惑.
任何建议将不胜感激!
我在heroku上的帐户被暂停,我正在尝试将其删除"cmd或git bash"; 但是,在我的程序中,我无法从该帐户注销.
这是我收到的错误:
$ heroku logout
C:/Users/Romantiku/.heroku/client/lib/heroku/cli.rb:30:in `rescue in start': und
efined method `styled_error' for Heroku::CLI:Class (NoMethodError)
from C:/Users/Romantiku/.heroku/client/lib/heroku/cli.rb:16:in `start'
from c:/Program Files (x86)/Heroku/bin/heroku:25:in `<main>'
Run Code Online (Sandbox Code Playgroud) 在TForm我们有HelpFile,HelpType和HelpKeyword属性.我们设置文件和关键字.当我们按F1时魔术来了.大家都知道.
但是,如果我们想要使用数据库keyword和help字段并创建新的自定义表单来显示帮助,使用HelpKeyword可视组件来知道要在自定义表单中显示哪个数据库记录,禁用标准Windows帮助系统.我们可以这样做吗?怎么样?
我在application_helper.rb中定义了一个助手,希望在邮件视图中使用它,但是只是在做
<%= helper_method(param) %>
Run Code Online (Sandbox Code Playgroud)
在mailer_view.html.erb文件中,显示“未定义方法”错误。
还有另一种方法吗?我讨厌不得不将同一个助手放在其他地方。
谢谢。
helpers ×10
ruby ×2
actionmailer ×1
activerecord ×1
delphi ×1
dynamic ×1
environment ×1
heroku ×1
model ×1
rspec ×1
rspec2 ×1
testing ×1
unit-testing ×1