百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

show_for

> 编程语言
开源

使用辅助对象包裹您的对象,以便轻松显示它们

463 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

使用辅助对象包裹您的对象,以便轻松显示它们

ShowFor

ShowFor allows you to quickly show a model information with I18n features.

erb

  
  
  
  
  

  
    
  

  
  

Installation

Install the gem:

gem install show_for

Or add ShowFor to your Gemfile and bundle it up:

gem 'show_for'

Run the generator:

rails generate show_for:install

And you are ready to go.

Note: This branch aims Rails 7 and 8 support, so if you want to use it with older versions of Rails, check out the available branches.

Usage

ShowFor allows you to quickly show a model information with I18n features.

erb

  
  
  
  
  

  
    
  

  

Will generate something like:

xml

  

  

  

  

  

  

You can also show a list of attributes, useful if you don't need to change any configuration:

erb

  

Value lookup

ShowFor uses the following sequence to get the attribute value:

  • use the output of a block argument if given
  • use the output of the :value argument if given
  • check if a :"human_#{attribute}" method is defined
  • retrieve the attribute directly.

Options

ShowFor handles a series of options. Those are:

  • :format - Sent to I18n.localize when the attribute is a date/time object.

  • :value - Can be used instead of block. If a Symbol is called as instance method.

  • :if_blank - An object to be used if the value is blank. Not escaped as well.

  • :separator - The piece of html that separates label and content, overriding the global configuration.

In addition, all containers (:label, :content and :wrapper) can have their html options configured through the :label_html, :content_html and :wrapper_html options. Containers can have their tags configured on demand as well through :label_tag, :content_tag and :wrapper_tag options.

Label

ShowFor also exposes the label method. In case you want to use the default human_attribute_name lookup and the default wrapping:

ruby
a.label :name                  #=> Name
a.label "Name", id: "my_name"  #=> Name

Optionally, if you want to wrap the inner part of the label with some text (e.g. adding a semicolon), you can do so by specifying a proc for ShowFor.label_proc that will be called with any label text. E.g.:

ruby
  ShowFor.label_proc = lambda { |l| l + ":" }

When taking this route, you can also skip on a per label basis by passing the :wrap_label option with a value of false.

Associations

ShowFor also supports associations.

erb

  
  
  

  
  
  

  
    

  

The first is a has_one or belongs_to association, which works like an attribute to ShowFor, except it will retrieve the artist association and try to find a proper method from ShowFor.association_methods to be used. You can pass the option :using to tell (and not guess) which method from the association to use.

:tags is a has_and_belongs_to_many association which will return a collection. ShowFor can handle collections by default by wrapping them in list ( with each item wrapped by an ). However, it also allows you to give :to_sentence or :join it you want to render them inline.

You can also pass a block which expects an argument to association. In such cases, a wrapper for the collection is still created and the block just iterates over the collection objects.

Here are some other examples of the many possibilites to custom the output content:

erb

  
    
  

  

Supported Ruby / Rails versions

We intend to maintain support for all Ruby / Rails versions that haven't reached end-of-life.

For more information about specific versions please check Ruby and Rails maintenance policies, and our test matrix.

Bugs and Feedback

If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.

http://github.com/heartcombo/show_for/issues

License

MIT License. Copyright 2020-CURRENT Rafael França, Carlos Antonio da Silva. Copyright 2012-2019 Plataformatec.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言