【问题标题】:Simple Form Select without Model没有模型的简单表单选择
【发布时间】:2013-06-24 22:52:02
【问题描述】:

我正在使用 Simple Forms (w/Rails 3.2),并希望创建一个带有 select 的表单。

例如:

Email Format :Text and HTML
             :Text Only
             :HTML Only

我不想为这样一个简单的选择创建一个带有关联的模型。如何在没有模型的情况下将其添加到表单中?

【问题讨论】:

    标签: ruby-on-rails simple-form


    【解决方案1】:
    f.input :email_format, :collection => ["Text and HTML", "Text Only", "HTML Only"], :prompt => "select email format"
    

    参考this

    【讨论】:

    • 在 Rails 4.1 中对我不起作用。未定义的方法“email_format”。似乎需要一个价值才能工作。作为简单的文本输入(f.input :email_format, input_html: {value: "Text and HTML"})。作为选择,您需要:= f.input :email_format, collection: ["Text and HTML", "Text Only", "HTML Only"], selected: "Text Only",:prompt => "select email format" to提供值以避免方法名称调用。
    【解决方案2】:

    <%= f.input :format, collection: ["Text and HTML", "Text only", "HTML only"] %> 但是,如果您不打算将其存储在任何模型中,应该可以工作,我不确定重点是什么。我会将它添加为您的电子邮件模型作为字符串。

    【讨论】:

      猜你喜欢
      • 2011-08-12
      • 1970-01-01
      • 2015-07-06
      • 2020-01-22
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 2012-05-02
      相关资源
      最近更新 更多