加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用返回字符串的Haml助手

发布时间:2020-12-17 03:20:08 所属栏目:百科 来源:网络整理
导读:我喜欢使用Haml助手,但多年来事情发生了一些变化.旧方法只是连接到缓冲区.这就是我所拥有的: def confirmation_table(field) # Be certain that if the user is logged in,his/her email and name show if field.respond_to? :user haml_tag('tr') { haml_t
我喜欢使用Haml助手,但多年来事情发生了一些变化.旧方法只是连接到缓冲区.这就是我所拥有的:

def confirmation_table(field)
  # Be certain that if the user is logged in,his/her email and name show
  if field.respond_to? :user
    haml_tag('tr') {
      haml_tag('th','Email:')
      haml_tag('td',field.user.email)
    }
    haml_tag('tr') {      
      haml_tag('th','Name:')
      haml_tag('td',field.user.full_name)
    }
  else
    haml_tag('tr') {
      haml_tag('th','User Information:')
      haml_tag('td','Not specified.')
    }
  end

  field.class.columns.collect{|col| col.name}.reject{|col| 
    col =~ /_at$/ || 
    col =~ /_on$/ ||
    col =~ /_id$/ ||
    col == 'id'}.each do |col|
    haml_tag('tr') {
      haml_tag('th',ActiveSupport::Inflector::humanize(col))
      haml_tag('td',typeize(field,col))
    }
  end
end

当然,这可以在我看来简单地访问:

- confirmation_table(@f)

但是,对我来说更有意义的是返回一个字符串.我看不出haml_capture如何提供相同的结构化能力.任何提示?

解决方法

将您的haml_tag调用包装在capture_haml中:

def confirmation_table(field)
  capture_haml do
    if field.respond_to? :user
      haml_tag(:tr) do
        haml_tag('th.email','Email:')
        haml_tag('td.email',field.user.email)
      end
      # ...
    end
  end
end

它们将被capture_haml捕获并返回.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读