数组不保留到数据库 [英] Array isn't persisted to database

查看:148
本文介绍了数组不保留到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在数据库中存储数组(。车辆)。这个阵列由用户选中的3个可能的车辆组成。复选框。

 <%@ vehicles.each_with_index do |车辆,索引| %GT; 
< label>
< div>
<%= check_box_tagvehicles [],index,class:available,style:display:none%>
<%=车辆%>
< / div>
< / label>
<%end%>

':车辆'属性类型是'text',我用这个代码使它成为一个数组:

  serialize:vehicles,array 

如果我发送参数,它将获取这些值在数组中。

 车辆= > [0,1,2],
提交=>注册!,
controller=>注册,
action=>create

问题是这个数组不存储在数据库当我尝试保存它。在数据库中,代码如下所示:

 车辆:[] 
pre>

控制器代码:

  def create 
@ student = Student.create(student_params)
如果@ student.save!
redirect_to @student
else
render:new
end
end

学生参数:

  def student_params 
params.require(:student).permit (availabilities_attributes:[:id,:day,... available])
end

一个想法如何将这些值纳入这个数组?



谢谢!

解决方案

在您的强大参数中,您将不得不允许:车辆属性作为数组,如下所示: vehicles:[]



我不知道您使用的是哪个版本的Devise,但是从他们的文档,在强参数部分,您可以在应用程序控制器中允许车辆 p>

  def configure_permitted_pa​​rameters 
devise_parameter_saniti zer.permit(:sign_up)do | student_params |
student_params.permit({vehicles:[]},:email,,password,,password_confirmation)
end
end

如果您使用Postgres数据库,我建议您设置车辆属性直接在数据库中接收数组。你可以这样进行迁移:

  class AddArrayToStudents< ActiveRecord :: Migration 
def change
add_column:students,:vehicles,:string,array:true,default:[]
end
end


I'm trying to store an array (:vehicles) in my database. This array consists of 3 possible vehicles that the user selects with checkboxes.

<% @vehicles.each_with_index do |vehicle,index| %>
    <label>
      <div >
        <%= check_box_tag "vehicles[]", index ,class: "available" ,style: "display:none" %>
        <%= vehicle %>
      </div>
    </label>
<%end %>

The ':vehicles' attribute type is 'text' and I used this code to make it an array:

serialize :vehicles, Array

If I send the params it gets these values in an array.

"vehicles"=>["0", "1", "2"],
"commit"=>"Sign up!", 
"controller"=>"registrations", 
"action"=>"create"

The problem is that this array isn't stored in the database when I try save it. In the database the code looks like this:

vehicles: []

Controller code:

 def create
    @student = Student.create(student_params)
    if @student.save!
      redirect_to @student
    else
      render :new
    end
  end

Student params:

def student_params
    params.require(:student).permit(availabilities_attributes: [ :id, :day, :available])
  end

Anyone have an idea how to get those values into this array?

Thanks!

解决方案

In your strong parameters you are going to have to permit the :vehicles attribute as an array, like this: vehicles: []

I'm not sure what version of Devise you are using, but drawing from their documentation, in the "strong parameters" section, you could permit vehicles like this in the application controller:

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up) do |student_params|
    student_params.permit({ vehicles: [] }, :email, :password, :password_confirmation)
  end
end

Also if you are using Postgres database, I recommend setting up your vehicles attribute to receive an array directly in the database. You can do so with a migration like this:

class AddArrayToStudents < ActiveRecord::Migration
  def change
    add_column :students, :vehicles, :string, array: true, default: []
  end
end

这篇关于数组不保留到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆