如何在字段中保存数组中的多个值?

时间:2016-02-28 20:01:37

标签: ruby-on-rails ruby

在模型项目中我有下一个数组:

PAYMENT_TYPE = ["在栏中"," bargeldlose Zahlung"," E-Geld"]

我的表项目包含字段付款,类型:字符串

当我创建新对象(模型项目)时,我如何在字段付款中添加数组PAYMENT_TYPE中的值?

在视图new.html.erb中我添加了

<%= Project::PAYMENT_TYPE.each do |type| %> 
      <%= f.check_box :payment, :name => 'project[payment][]', type %>
    <% end %>

显示错误: /home/ubuntu/workspace/app/views/projects/new.html.erb:35:语法错误,意外&#39;)&#39;,expecting =&gt; ... =&GT; &#39;项目[付款] []&#39;,输入); @ output_buffer.safe_append =&#39; ...

完整的new.html.erb:

<%= form_for @project do |f| %>
  <p>
    <%= f.label 'Name' %>
    <%= f.text_field :title %>
  </p>
  <P>
    <%= f.label 'Budget' %>
    <%= f.text_field :price %>

    für

    <%= f.select :price_category, Project::PRICE_CATEGORIES %>
  </P>
  <p>
    <%= f.label 'Description' %>
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label 'Category' %>
    <%= f.collection_select :category_id, Category.all, :id, :name %>
  </p>

  <p>
    <%= f.label 'Skills Freelancer (15 pieces) *' %>
    <%= f.text_field :skill %>
  </p>

  <p>
    <%= f.label 'Location' %>
    <%= f.text_field :location %>
  </p>

  <p>
    <%= Project::PAYMENT_TYPE.each do |type| %> 
      <%= f.check_box :payment, :name => 'project[payment][]', type %>
    <% end %>
  </p>

  <p>
    <b>Anonymity order</b> 
  </p>
  Setup allows for players to remain anonymous. Note that this may reduce the number of responses.

  <p>
    <%= f.label 'Place Order anonymously' %>
    <%= f.check_box :anonymity %>    
  </p>

  <p>
    <%= f.fields_for :documents do |d| %>
      <%= f.label :attachment %>
      <%= f.file_field :attachment %>
    <% end %>
  </p>


  <P>
    <%= f.submit 'Create' %>    
  </P>

<% end %>

1 个答案:

答案 0 :(得分:0)

我不能谷歌这个表单的正确语法,但除了在Ruby中的方法调用的最后一个参数之外,你不能像你一样使用裸哈希参数。也就是说,你的代码是无效的Ruby。