- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试创建一个辅助函数(例如在 application_helper.rb
中),它根据传递给辅助函数的参数生成一个 link_to
。如果我将链接放在 ERB 文件中,它将采用以下格式:
<%= link_to 'Some Text', { :controller => 'a_controller', :action => 'an_action' } %>
在helper函数中,Text、Controller、Action都是传入或者计算出来的。辅助函数中的代码是:
params = "{ :controller => '#{controller}', :action => '#{action_to_take}'}"
html = "#{link_to some_text, params }<p />"
return html
生成的链接具有正确的文本,但参数实际上是 params
字符串的内容。
如何获取要评估的 params
字符串(因为它在 ERB 文件中)?
最佳答案
我认为您脑子里的事情过于复杂了。除非你想做一些非常奇怪(而且不可取)的事情,否则这会起作用:
def link_helper text, controller, action
link_to text, :controller => controller, :action => action
end
不过,如您所见,不值得将其作为助手 - 它几乎不比它包装的功能简单,而且灵 active 要差得多。
link_to 帮助器返回一个字符串,因此按照您想要的方式使用它非常简单:
def link_helper text
# some helper logic:
controller = @controller || "pages"
action = @action || "index"
# create the html string:
html = "A link to the #{controller}##{action} action:"
html << link_to(text, :controller => controller, :action => action)
html # ruby does not require explicit returns
end
关于ruby-on-rails - Rails View 辅助函数和 link_to 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668616/
link_to 方法没有被禁用:- 但如果我喜欢下面隐藏链接 现在的问题是如何用块禁用这种类型的链接,以及第二个代码有效而第一个无效的原因是什么。 最佳答案 也
这是一个有点棘手的情况。我正在测试我最近容器化的 Laravel 应用程序的部署。我制作了一个基于 php 的容器,它在自身内部运行 Apache 来为应用程序提供服务。如果我只是运行这个容器,绑定(
您好,我必须在 Active Admin 管理页面中创建一些链接,但是我在理解哈希值以搜索特定条目时遇到了一些问题,例如: column 'Campaigns' do |advertiser| l
我希望从一个字段中提取一个十六进制值到 rails 的 link_to 行中。我找到了如何根据以下内容格式化标准链接 'color:#FF6A00;', :class => 'css_class'}
例子: 代码: <%= link_to "Issues", {:controller => "issue", :actio
我真的很难理解如何使用循环链接到父级。 我的里程碑属于我的订单,我的订单有很多里程碑。 在我的订单索引中,我有一个简单的日历 (table_builder),其中列出了我的所有里程碑。 @date.
可以放request.referer吗在 link_to标签。我有一个页面被两个不同的页面调用,我想使用一个链接返回(如果有一个返回页面,否则,放一个默认的)。到目前为止,我尝试过这个。 但是我
我已经创建了一个对象 Events,并且重构了 Controller 。现在我有了: 观点: 路线: scope 'admin', :module => 'back', :as => 'back'
(虽然这里讨论的是 Blacklight 引擎,但我相信这个问题实际上纯粹是关于 Rails 的。) 继续我的追求 scope around mount ineffective? (tl;dr:在挖掘
我正在编写一个 ruby-on-rails 库模块: module Facets class Facet attr_accessor :name, :display_name, :ca
我必须在不使用CSS类的情况下更改link_to标签的颜色,怎么办?我已经尝试过诸如 'color:#FFFFFF;', :class => "css_class"} %> 但在ruby 1.9.2和
使用 Rails 3,有没有办法在模型内部使用 link_to 帮助器或任何与此相关的帮助器? 最佳答案 出于某些原因,您可能需要在模型中使用 link_to。是的,@andy,这违反了 MVC,但这
我很难将查询字符串参数添加到 link_to UrlHelper。例如,我有一个索引 View ,它具有用于排序、过滤和分页(通过 will_paginate)的 UI 元素。 will_pagina
我正在开发一个 Rails v2.3.2 应用程序。 我有一个 Controller : class SchoolController :check_teachers, :id => @school
我有一个显示交易搜索结果的页面,并且我有一个基于一列( token )的搜索过滤器,单击此后应设置搜索条件,并且应在 post 方法中将搜索参数作为表单数据传递。但目前,它被附加到 URL 中。请帮助
我有一个链接列表,单击时我希望它通过 Ajax 显示其相关部分,我已成功呈现第一个链接,但希望实现一些 if 语句来识别哪个 link_to 已被单击。如何区分链接? true %>
我在index.html.erb页面上有一个链接。它是一个下载链接。现在我希望如果光标悬停在链接上,则会显示工具提示文本。e我尝试了几种组合,但还没有得到解决方案。 我的代码在这里: Downloa
[更多信息见后面的回答] 我认为这只是一个简单的 Rails 问题,主要是关于我如何命名我的模型并在我的 View 中引用它们。所以有点背景,我正在使用 vote_fu插件,我认为它很棒,但是我很难通
我想知道 :with 属性是否已从 rails3 中删除,因为我在 rails3 api 中找不到任何内容 - http://rails3api.s3.amazonaws.com 任何人都有关于如何使
我正在使用 RoR 创建简单的应用。 我有一个看起来像这样的链接 当鼠标悬停在图标上时,我再也看不到图标了,我只能看到图标大小的黑色方 block 。我能够设置访问的颜色并且它工作正常但我无法更改
我是一名优秀的程序员,十分优秀!