【问题标题】:Dynamically set form placeholder text with helpers in Rails 4在 Rails 4 中使用助手动态设置表单占位符文本
【发布时间】:2015-04-02 09:09:52
【问题描述】:

我正在使用不同的帖子“模板”构建应用程序。除了占位符文本之外,帖子的每个表单都是相同的。我试图弄清楚如何动态设置占位符文本。我一直在尝试将所有模板助手命名为相同的名称,最后保留 @template.id。我似乎无法让动态功能工作。

 def title_template_1
      'Placeholder for Template 1 Title'
   end

  def title_template_2
  'Placeholder for Template 2 Title'
  end


 <%=    f.text_field :title, 
:placeholder => title_template_1 %> 

 <%=    f.text_field :title, 
:placeholder => title_template_'#{@template.id}' %>#this doesn't work, but I'm showing it to show the functionality I'm trying to achieve.

谢谢

【问题讨论】:

    标签: ruby-on-rails forms dynamic helper placeholder


    【解决方案1】:

    试试这个而不是title_template_'#{@template.id}'

    :placeholder => send("title_template_#{@template.id}")
    

    【讨论】:

    • 啊!谢谢!我无法弄清楚......现在我明白了为什么。我学到了一些新东西。
    猜你喜欢
    • 2015-02-27
    • 1970-01-01
    • 2014-06-16
    • 2014-10-29
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多