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

ruby-on-rails – Rails – 将选项传递给方法

发布时间:2020-12-16 23:22:15 所属栏目:百科 来源:网络整理
导读:我有一个看起来像这样的帮助方法: def html_format(text,width=15,string="wbr /",email_styling=false) if email_styling ...... stuff else ...... stuff end ...... stuff end 我在发送email_styling时遇到问题.以下是我在视图中所做的事情: %= html_fo
我有一个看起来像这样的帮助方法:
def html_format(text,width=15,string="<wbr />",email_styling=false)

    if email_styling
       ...... stuff
    else
       ...... stuff
    end
       ...... stuff
  end

我在发送email_styling时遇到问题.以下是我在视图中所做的事情:

<%= html_format(@comment.content,:email_styling => true) %>

我错误地传递了真的吗?谢谢

解决方法

你没有正确传递它.您需要执行以下操作:
<%= html_format(@comment.content,15,'<wbr />',true) %>

或者,您可以使用选项哈希来传递参数:

def html_format(text,options = {})
  opt = {:width => 15,:string => '<wbr />',:email_styling => false}.merge(options)

  if opt[:email_styling]
    ...
  end
end

这样你就可以这样打电话:

<%= html_format(@comment.content,:email_styling => true) %>

(编辑:李大同)

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

    推荐文章
      热点阅读