开发者

create custom html helpers in ruby on rails

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" featur开发者_Python百科e in ASP.NET MVC So I can create my own html helper

<%= Html.MyOwnHtmlHelper() %>

I've learned that there is html helpers in Ruby such as

<% text_area %>

which render at html

I have a question. Can I create my own html helper for rendering my own html?


To create a new helper:

  1. choose a name for the helper file, for instance tags_helper.rb
  2. create the file in the /app/helpers directory
  3. create a module according to the file name. In this case

    module TagsHelper
    end
    
  4. define your helper as method

    module TagsHelper
      def hello_world(name)
        "hello #{name}"
      end
    end
    

Now you can use the hello_world helper method in your view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜