【问题标题】:What is the proper Ruby syntax for values in select option form?选择选项表单中值的正确 Ruby 语法是什么?
【发布时间】:2015-06-09 22:46:17
【问题描述】:

我有一个表单,它正在向具有嵌套属性的模型提交数据。我正在使用 select 从选项列表中进行选择。

Articles 模型为 Documents 模型接受_nested_attributes_,该模型具有称为 Attachment 的_attached_file。附件的 url_path 是选择字段应该选择的内容。

如何更改选择字段的值?

HTML 现在看起来像这样:

<select id="article_document" name="article[document]">
    <option>...</option>
</select>

但我想要这个:

<select id="article_document_url_path" name="article[document][url_path]">
    <option>...</option>
</select>

Ruby 看起来像这样:

<%= form_for @article, :html => { :multipart => true } do |f| %>

    <%= f.select( :document,
              options_for_select(@article.attachment_list.map{ |c| [c, {'data-img-src' => c }] }) ) %>
<% end %>

Rails 4.1.8,红宝石 2.1.5p273

【问题讨论】:

    标签: html ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    试试这个:

    <%= form_for @article, :html => { :multipart => true } do |f| %>
        <%= f.fields_for :document do |doc|%>
            <%= doc.select(
                :url_path,  
                options_for_select(
                    @article.attachment_list.map{ |c| [c, {'data-img-src' => c }] }
                ) 
            ) %>
        <% end %>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 2022-11-13
      • 2012-05-15
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 2017-09-22
      相关资源
      最近更新 更多