表单输入中的字符串插值

时间:2017-08-09 07:02:05

标签: ruby

在Rails中,我尝试为多种视频类型创建视频上传表单。

我用这样的代码结束了:

= semantic_form_for element, url: form_url, method: form_method, remote: true do |f|
  // there will be some form should return once
  - ['webm', 'mp4', 'ogv'].each do |ext|
    .video-item-uploader
      = f.input :"#{ext}", hint: [ f.object."#{ext}"? ? "#{I18n.t('uploaded')}" : '' ].join.html_safe
      = f.input :"#{ext + '_cache'}", as: :hidden

      = f.input :_destroy, as: :boolean, label: "#{I18n.t('do_delete')}"

使用这样的代码,我遇到了语法错误。如果我用数组中的任何文本(没有引号)替换"#{ext}",它就可以工作。

是否有可能在表单输入中插入变量?

1 个答案:

答案 0 :(得分:2)

= semantic_form_for element, url: form_url, method: form_method, remote: true do |f|
  // there will be some form should return once
  - %w(webm mp4 ogv).each do |ext|
    .video-item-uploader
      = f.input :"#{ext}", hint: [ f.object.send("#{ext}?")? ? "#{I18n.t('uploaded')}" : '' ].join.html_safe
      = f.input :"#{ext + '_cache'}", as: :hidden

      = f.input :_destroy, as: :boolean, label: "#{I18n.t('do_delete')}"

试试这个