具有连接表属性的嵌套表单

时间:2015-07-05 14:17:21

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

我有一个新的比赛表格: 编辑:

= form_for(@match) do |f|
  = f.label :match_date
  = f.date_select :match_date, :order => [:day, :month, :year]
  = f.label :player_ids, 'Select players'
  = f.collection_select :player_ids, @players, :id, :lastname, {}, { multiple: true }

  = f.fields_for :match_edits do |ff|
    = ff.label :result
    = ff.number_field :result, in: 0..10
  %div
    = f.button :submit

当我选择两个玩家时,我想为每个匹配结果设置如下:

  

id:1,match_id:1,player_id:1,结果:4

     

id:2,match_id:1,player_id:2,结果:10

我是rails的新手,我不知道如何修复

MatchController

class MatchesController < ApplicationController

  respond_to :html

  def index
    @matches = Match.all
  end

  def show
    @match = Match.find(params[:id])
    @results = @match.match_edits
  end

  def new
    @match = Match.new
    @players = Player.all
    2.times {@match.match_edits.build}
  end

  def create
    @match = Match.new(match_params)
    respond_to do |format|
      if @match.save
        format.html { redirect_to @match, notice: 'Match was successfully created.' }
        format.json { render :show, status: :created, location: @match }
      else
        format.html { render :new }
        format.json { render json: @match.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def match_params
    params[:match].permit :match_date, player_ids: [], :match_edits_attributes => [:id, :result]
  end

end

MatchEdit模型

class MatchEdit < ActiveRecord::Base

    belongs_to :match
    belongs_to :player

end

匹配模型

class Match < ActiveRecord::Base

    has_many :match_edits
    has_many :players, through: :match_edits
    accepts_nested_attributes_for :match_edits, allow_destroy: true, reject_if: proc { |attrs| attrs['result'].blank? }

end

Schema.rb

ActiveRecord::Schema.define(version: 20150629144534) do

  create_table "match_edits", force: :cascade do |t|
    t.integer  "match_id"
    t.integer  "player_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "result"
  end

  add_index "match_edits", ["match_id"], name: "index_match_edits_on_match_id"
  add_index "match_edits", ["player_id"], name: "index_match_edits_on_player_id"

  create_table "matches", force: :cascade do |t|
    t.date     "match_date"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "players", force: :cascade do |t|
    t.string   "firstname"
    t.string   "lastname"
    t.string   "picture"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

2 个答案:

答案 0 :(得分:0)

您需要更新表单以删除冗余代码,例如:

= ff.number_field :result, in: 0..10

您的表单如下:

= form_for(@match) do |f|
  = f.label :match_date
  = f.date_select :match_date, :order => [:day, :month, :year]
  = f.label :player_ids, 'Select players'
  = f.collection_select :player_ids, @players, :id, :lastname, {}, { multiple: true }

  = f.fields_for :match_edits do |ff|
    = ff.label :result
    = ff.number_field :result, in: 0..10
  %div
    = f.button :submit

您的控制器的new方法负责为result提供多个字段:

class MatchsController << ApplicationContoller
  ...

  def new
    ...
    2.times { @match.match_edits.build }
    ...
  end

  ...
end

您的模型应允许接受嵌套属性,如下所示:

class Match
  ...

  has_many :match_edits
  accepts_nested_attributes_for :match_edits, allow_destroy: true,
                                 reject_if: proc { |attrs| attrs['result'].blank? }

  ...
end

class MatchEdit
  ...

  belongs_to :match

  ...
end

答案 1 :(得分:0)

我找到了解决方案。表格应如下所示:

= form_for(@match) do |f|
  = f.label :match_date
  = f.date_select :match_date, :order => [:day, :month, :year]

  = f.fields_for :match_edits do |ff|
    = ff.label :player_id, 'Select player'
    = ff.collection_select :player_id, @players, :id, :lastname, {}, { multiple: false }
    = ff.label :result
    = ff.number_field :result, in: 0..10
  %div
    = f.button :submit

并在matches_controller中:

 def match_params
    params[:match].permit :match_date, :match_edits_attributes => [:id, :result, :player_id]
  end