User Params not taking data as an array in rails using boostrap multi select dropdown

问题: I have a user model with various attributes. Of them I am using bootstrap multi-select dropdown to get multi selected values for two fields. I am not able to parse those tw...

问题:

I have a user model with various attributes. Of them I am using bootstrap multi-select dropdown to get multi selected values for two fields. I am not able to parse those two fields and receive them as params both which hold array values of the multi-selected options and store the same as an array value into the database

This is my User model

 class User < ApplicationRecord
   serialize :type_of_brand, Array
   serialize :amenities_provided, Array
 end

The _form.html.erb for user view:

<div class="field"> 1.
  <%= form.label :name %> :
  <%= form.text_field :name, id: :user_name %>
</div>

<div class="field"> 2.
  <%= form.label :phone %> :
  <%= form.text_field :phone, id: :user_phone %>
</div>

<div class="field"> 3.
  <%= form.label :email %> :
  <%= form.text_field :email, id: :user_email %>
</div>

<div class="field"> 9.
  <label> Types of Packaging Water Brand Sold </label><br/>
  <select id="type_of_brand" multiple="multiple" 
  name="type_of_brand[]">
    <option value="freezer">Bisleri</option>
    <option value="delivery">Aquafina</option>
    <option value="freezer and delivery">Kinley</option>
    <option value="Incentives">Bindu</option>
  </select>
</div>

<div class="field"> 17.
  <label> Amenities provided </label><br/>
  <select id="amenities_provided" multiple="multiple" 
  name="amenities_provided[]">
    <option value="freezer">Freezer</option>
    <option value="delivery">Delivery</option>
    <option value="freezer and delivery">Freezer and Delivery</option>
    <option value="Incentives">Incentives</option>
  </select>
</div>

<div class="field"> 18.
  <%= form.label 
  :number_of_deliveries_done_by_your_distributor_in_a_month %> :
  <%= form.number_field :total_delivered_by_distributor, id: 
  :user_total_delivered_by_distributor %>
</div>

<div class="actions">
  <%= form.submit %>
</div>

<div class="actions">
  <input class="btn btn-danger" type="reset" value="Reset">
</div>

<% end %>
</div>

<script type="text/javascript">
  $(document).ready(function() {
    $('#type_of_brand, #amenities_provided').multiselect();
  });
</script>

I get the following params

{"name"=>"Mahesh", "phone"=>"9008849042", 
"email"=>"Alexander9@teamyogi.com"} 
permitted: true>

Here I am not able to get the type_of_brand and amenities_provided params at all which I am expecting in arrays. What am I doing wrong?.


回答1:

Ok I figured the solution Here it goes

<div class="field"> 9.
  <label> Types of Packaging Water Brand Sold </label><br/>
  <select id="type_of_brand" multiple="multiple" 
  name="user[type_of_brand][]">
    <option value="freezer">Bisleri</option>
    <option value="delivery">Aquafina</option>
    <option value="freezer and delivery">Kinley</option>
    <option value="Incentives">Bindu</option>
  </select>
</div>

<div class="field"> 17.
  <label> Amenities provided </label><br/>
    <select id="amenities_provided" multiple="multiple" 
    name="user[amenities_provided][]">
      <option value="freezer">Freezer</option>
      <option value="delivery">Delivery</option>
      <option value="freezer and delivery">Freezer and Delivery</option>
      <option value="Incentives">Incentives</option>
   </select>
</div>

Also allow in the strong parameters in users controller

params.require(:user).permit(:name, :phone, :email, :type_of_brand => [], 
:amenities_provided => [])

That should do the trick

  • 发表于 2019-01-01 14:40
  • 阅读 ( 216 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除