ruby on rails - Nested forms with join table attributes -


i have new match form: edit:

= form_for(@match) |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 |ff|     = ff.label :result     = ff.number_field :result, in: 0..10   %div     = f.button :submit 

when choose 2 players want set each 1 match result this:

id: 1, match_id: 1, player_id: 1, result: 4

id: 2, match_id: 1, player_id: 2, result: 10

i'm new in rails , don't know how fix that

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 |format|       if @match.save         format.html { redirect_to @match, notice: 'match 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 model

class matchedit < activerecord::base      belongs_to :match     belongs_to :player  end 

match model

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)    create_table "match_edits", force: :cascade |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 |t|     t.date     "match_date"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end    create_table "players", force: :cascade |t|     t.string   "firstname"     t.string   "lastname"     t.string   "picture"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end  end 

you need update form remove redundant code i.e.:

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

your form like:

= form_for(@match) |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 |ff|     = ff.label :result     = ff.number_field :result, in: 0..10   %div     = f.button :submit 

your controller's new method responsible to provide multiple fields result:

class matchscontroller << applicationcontoller   ...    def new     ...     2.times { @match.match_edits.build }     ...   end    ... end 

your model should allow accept nested attributes following:

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 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -