在 Rails 4 中使用助手动态设置表单占位符文本
Dynamically set form placeholder text with helpers in Rails 4
我正在构建一个具有不同 post "templates" 的应用程序。 Post 的每个形式除了占位符文本外都是相同的。我想弄清楚如何动态设置占位符文本。我一直在尝试将所有模板助手命名为相同的名称,最后保存一个 @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.
谢谢
试试这个而不是 title_template_'#{@template.id}'
:placeholder => send("title_template_#{@template.id}")
我正在构建一个具有不同 post "templates" 的应用程序。 Post 的每个形式除了占位符文本外都是相同的。我想弄清楚如何动态设置占位符文本。我一直在尝试将所有模板助手命名为相同的名称,最后保存一个 @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.
谢谢
试试这个而不是 title_template_'#{@template.id}'
:placeholder => send("title_template_#{@template.id}")