Rails数组,数据库和表单

时间:2016-03-03 19:15:30

标签: ruby-on-rails arrays

我正在创建一个允许用户创建或加入团队的应用程序。团队拥有一系列技能,一系列类别和一系列成员。我遇到的问题是每当我保存到数据库时,它都不会保存为数组。我根据Cat模型选择填充猫场。我有另一个项目,我在其中创建了一个数组列,如果我使用控制台Model_name.find(1),它将显示属性" --- []"表示它是一个数组。我无法弄清楚为什么这个新应用程序不关心它是一个数组的事实(因为根据输出,它不是,但根据迁移它是)。我现在已经搜索了大约3天但无济于事。同样的解决方案"除了我之外,这对每个人都有用。

--- controller -------

class TeamsController < ApplicationController
  before_action :set_team, only: [:show, :edit, :update, :destroy]

  # GET /teams
  # GET /teams.json
  def index
   @teams = Team.all
  end

  # GET /teams/1
  # GET /teams/1.json
  def show
    @owner = User.find(@team.user_id)
    @member = User.new
    @members = User.where(team_id: @team.id).where.not(team_role: "owner")
    @cats = []
  end

  # GET /teams/new
  def new
    @team = Team.new
  end

  # GET /teams/1/edit
  def edit
  end

  # POST /teams
  # POST /teams.json
  def create
    @team = Team.new(team_params)

    respond_to do |format|
      if @team.save
        user = User.find(@team.user_id)
        user.team_id = @team.id
        user.team_role = "owner"
        user.save
        format.html { redirect_to @team, success: 'Team was successfully created.' }
        format.json { render :show, status: :created, location: @team }
      else
        format.html { render :new }
        format.json { render json: @team.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /teams/1
  # PATCH/PUT /teams/1.json
  def update
    respond_to do |format|
      if @team.update(team_params)
        format.html { redirect_to @team, success: 'Team was successfully updated.' }
        format.json { render :show, status: :ok, location: @team }
      else
        format.html { render :edit }
        format.json { render json: @team.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /teams/1
  # DELETE /teams/1.json
  def destroy
    @team.destroy
    @users = User.where(team_id: @team.id)
    @users.each do |u|
      u.team_id = nil
      u.team_role = nil
      u.save
    end
    respond_to do |format|
      format.html { redirect_to teams_url, notice: 'Team was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_team
      @team = Team.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def team_params
      params.require(:team).permit(:name, :hiring, :user_id, :skills, :cats, :members, :tokens)
    end

end
--- model ------
class Team < ActiveRecord::Base
  belongs_to :user
end
<%= form_for(@team) do |f| %>    
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :hiring %><br>
    <%= f.check_box :hiring %>
  </div>
  <div class="field">
    <%= f.label :cats, "Select a Primary Category" %><br>
    <%= f.select :cats, Cat.all.collect {|a| [a.name, a.id]},{},{ class: 'form-control'} %>
  </div>
  <%= f.hidden_field :user_id, value: current_user.id %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

您需要使用serializerails序列化/反序列化它们。

首先,您需要确保数据库中这些字段的column_typetext

然后serialize这些字段。

class Team < ActiveRecord::Base
  belongs_to :user
  serialize :cats, Array
end

有关详细信息,请参阅ActiveRecord.serialize