Tag: 助手

在所有rails帮助程序中将协议更改为https

Rails 3.1+我希望我的url帮助程序使用https协议,而不必在我调用的每个帮助程序中指定它。 搜索后我发现了各种各样的方法但没有工作,例如: ROUTES_PROTOCOL = (ENV[“RAILS_ENV”] =~ /development/ ? ‘http://’ : ‘https://’) scope :protocol => ROUTES_PROTOCOL, :path => “/app” do 如何才能做到这一点?

将变量传递给’helper_method’

我正在使用Ruby on Rails 3.0.9,我想将一些变量传递给我的控制器中声明的helper_method 。 class ArticlesController < ApplicationController helper_method :method_name private def method_name # Here I would like to state\use something like 'def method_name(var1, var2)' … end end 可能吗? 如果是这样,我应该如何编码以及如何使用它?

Rails助手不在测试环境中工作

我已经按照http://railscasts.com/episodes/221-subdomains-in-rails-3上的教程进行了操作。 它允许您通过覆盖帮助文件中的url_for方法将子域选项传递给路由。 我的帮手方法看起来像这样: module UrlHelper def with_subdomain(subdomain) subdomain = (subdomain || “”) subdomain += “.” unless subdomain.empty? [subdomain, request.domain, request.port_string].join end def url_for(options = nil) if options.kind_of?(Hash) && options.has_key?(:subdomain) options[:host] = with_subdomain(options.delete(:subdomain)) end super end end 所以: sites_homepage_url(:subdomain => “cats”) 生成url: “http://cats.example.com/sites/1/homepage” 这在开发中工作得很好。 然而,在我的黄瓜测试中,使用: sites_homepage_url(:subdomain => “cats”) 生产: “http://www.example.com/sites/1/homepage?subdomain=cats” 这表示我在助手中添加到url_for的function不起作用。 有人有任何想法吗? 编辑:格式化并添加UrlHelper的代码。

为什么仍然可以在视图中访问私有帮助方法?

只是另一个“为什么会那样”的问题:我注意到私有帮助器方法仍然可以在视图中访问。 为什么? 有没有办法防止这种情况(例如,当只有从另一个帮助器中调用辅助方法时)?

从Rails Helper返回多个标签的最佳方法是什么?

我想创建一个隐藏字段并在一个帮助器中创建一个链接,然后输出到我的erb。 应该把结果拿出来 link_to “something”, a_path form.hidden_field “something”.tableize, :value => “something” 帮助者的定义是什么样的? link_to和form.hidden_​​field的细节并不重要。 重要的是,如何从两个不同的调用返回输出。

在rails中生成javascript的完整URL(类似于javascript_path,但是url)

如何生成javascript文件的绝对链接。 我假设应该有类似下面的那个(遗憾的是似乎没有): javascript_url ‘main’ # -> ‘http://localhost:3000/javascripts/main.js’ 代替: javascript_path ‘main’ # -> ‘/javascripts/main.js’ 我需要绝对URL,因为该javascript文件将用于书签。 另外我需要相同的css文件。 谢谢, 德米特里。

Rails从Helper模块返回多个content_tags

我写了以下帮助: def section_to_html (block) case block[0].downcase when “paragraph” block.shift block.each do |value| return content_tag(:p, value) end end end 它目前正在解析这些数组。 [“paragraph”, “This is the first paragraph.”] [“paragraph”, “This is the second.”, “And here’s an extra paragraph.”] 它返回: This is the first paragraph. This is the second. 有没有办法累积content_tag? 所以它返回: This is the first paragraph. This is the […]

如何在普通旧ruby中使用rails helper“distance_of_time_in_words”(非导轨)

Rails有一个帮助器,它使用distance_of_time_in_words转换单词的时间距离 如果我只是在irb提示符下使用ruby来获取此function,我需要包括什么? 我试过require ‘action_view’但没有帮助。 我也尝试过require ‘active_support/core_ext’但这也没有帮助。

railsstutorial.org中的SessionsHelper:帮助者应该是视图中不需要的代码的通用模块吗?

railstutorial.org有一个建议让我觉得有点奇怪。 它建议这段代码 : class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper end include SessionsHelper使得方法可以从ApplicationController ,是的,但它也可以在任何视图中使用它们。 我知道认证/授权是跨领域的,但这真的是最好的地方吗? 在我看来,这似乎可能过于宽泛。 在一个更常见的包含视图帮助程序的模块中放置实现比较重定向的before_filter代码(如railstutorial.org示例所示)似乎令人惊讶。 在视图中不严格需要的function是否可以更好地放在ApplicationController或其他地方? 或者我只是在考虑这个问题?

通过访问原始文件覆盖rails帮助程序

我想使用rails熟悉的助手,但function略有改变。 我看待它的方式,我希望能够做到这样的事情: module AwesomeHelper #… create alias of stylesheet_link_tag to old_stylesheet_link_tag def stylesheet_link_tag(*args) if @be_awesome awesome_stylesheet_link_tag *args else old_stylesheet_link_tag *args end end end 我看到它的方式,我有三个选择: 猴子修补:重新打开rails helper模块。 如果rails团队改变了他们的帮助器模块的名称,我的代码就变成了脆弱的来源。 不是不可克服的,但并不理想。 使用不同的方法名称:试图坚持共轨接口可能是我的垮台。 我的更改可能会成为其他开发人员混淆的根源 分离方法(新):不确定这是否有效,或者它是否会有与1相同的缺点。研究这个,但这可能是一个很好的起点。 所以这里的问题是,我是否坚持使用其中一种次优解决方案,还是有其他方式我没有考虑过? 如果我选择选项3,有没有办法在不直接寻址rails helper模块的情况下执行此操作? (注意:我删除了上下文,因为它没有添加任何问题。)