在 ruby on rails 中创建自定义 html 助手 [英] create custom html helpers in ruby on rails
问题描述
我在一年前开始在 ASP.NET MVC 框架上编程.最近.我正在学习 Ruby On Rails 框架ASP.NET MVC 中有自定义 html 助手"功能所以我可以创建我自己的 html helper
I've starting programming on ASP.NET MVC Framework a year ago. Recently. I've learning Ruby On Rails Framework There is "custom html helper" feature in ASP.NET MVC So I can create my own html helper
<%= Html.MyOwnHtmlHelper() %>
我了解到 Ruby 中有 html 助手,例如
I've learned that there is html helpers in Ruby such as
<% text_area %>
在 html 中呈现
我有一个问题.我可以创建自己的 html helper 来呈现我自己的 html 吗?
I have a question. Can I create my own html helper for rendering my own html?
推荐答案
要创建一个新的助手:
- 为帮助文件选择一个名称,例如 tags_helper.rb
- 在/app/helpers 目录中创建文件
根据文件名创建模块.在这种情况下
- choose a name for the helper file, for instance tags_helper.rb
- create the file in the /app/helpers directory
create a module according to the file name. In this case
module TagsHelper
end
将您的助手定义为方法
define your helper as method
module TagsHelper
def hello_world(name)
"hello #{name}"
end
end
现在您可以在视图中使用 hello_world
辅助方法.
Now you can use the hello_world
helper method in your view.
这篇关于在 ruby on rails 中创建自定义 html 助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!