Rails:通过关联创建动作而不是保存has_many - "回滚事务"

时间:2016-08-26 16:04:42

标签: ruby-on-rails forms has-many-through

对我来说很容易,我刚刚开始学习Rails,这是我在这里的第一个问题!

我用来学习的项目是一个排球记分牌,所以现在我试图建立一个提交2v2比赛得分的表格。我有 用户 游戏 ,它们通过与 <的联接表的has_many关联关联em>参与者 ,其中还包含一个&#39;结果&#39;属性(&#39; W&#39;或&#39; L&#39;)。

我的问题是,当我提交时失败,并且没有创建 参与者 。如果我从表单中删除了关联,则提交将仅使用游戏参数。

希望我已经包含了以下所有相关信息。此外,如果有更好的方法来做这一切,我很乐意听到它!

模型

class Game < ApplicationRecord
  has_one :venue
  has_many :participants
  has_many :users, through: :participants

  accepts_nested_attributes_for :participants,
    reject_if: :all_blank, allow_destroy: true
end

class User < ApplicationRecord
  has_many :participants
  has_many :games, through: :participants
end

class Participant < ApplicationRecord
  belongs_to :game
  belongs_to :user
end

SCHEMA

create_table "games", force: :cascade do |t|
    t.date     "game_date"
    t.integer  "winning_score"
    t.integer  "losing_score"
    t.text     "notes"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.integer  "venue_id"
    t.index ["venue_id"], name: "index_games_on_venue_id"
end

create_table "participants", force: :cascade do |t|
  t.integer  "user_id"
  t.integer  "game_id"
  t.string   "result"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["game_id"], name: "index_participants_on_game_id"
  t.index ["user_id"], name: "index_participants_on_user_id"
end

create_table "users", force: :cascade do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at",      null: false
  t.datetime "updated_at",      null: false
  t.string   "password_digest"
  t.string   "remember_digest"
  t.index ["email"], name: "index_users_on_email", unique: true
end

create_table "venues", force: :cascade do |t|
  t.string   "name"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

CONTROLLER

class GamesController < ApplicationController

  def show
    @game = Game.find(params[:id])
  end

  def new
    @users = User.all
    @game = Game.new
    @game.participants.build
  end

  def create
    @game = Game.new(game_params)

    if @game.save
      redirect_to 'show'
    else
      render 'new'
    end
  end

  private

    def game_params
      params.require(:game).permit(:game_date, :winning_score,
                                   :losing_score, :notes, :venue_id,
                                   participants_attributes: [:user_id, :result, 
                                   :_destroy])
    end

end

FORM

<%= simple_form_for @game do |f| %>
  <div id="winners">
    <b>Winners</b>
    <% for i in 0..1 %>
      <%= f.simple_fields_for :participants do |p| %>
        <%= p.association :user, :collection => @users, label: false %>
        <%= p.input :result, :as => :hidden, :input_html => { :value => 'W' }%>
      <% end %>
    <% end %>
  </div>

  <%= f.input :winning_score, :collection => 15..30 %>

  <div id="losers">
    <b>Losers</b>
    <% for i in 2..3 %>
      <%= f.simple_fields_for :participants do |p| %>
        <%= p.association :user, :collection => @users, label: false %>
        <%= p.input :result, :as => :hidden, :input_html => { :value => 'L' }%>
      <% end %>
    <% end %>
  </div>

  <%= f.input :losing_score, :collection => 0..30 %>

  <%= f.input :notes %>

  <%= f.submit "Submit!", class: "btn btn-primary" %>

<% end %>

RESPONSE

Processing by GamesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"p8081+wU7EqYV7PIIAOGP3N+Md4CJusFpL9qTm3CeC54fP7pTPEwtfYS5v5x+ErBWxGiB0oj1pklYGXwl/cRBw==", "game"=>{"participants_attributes"=>{"0"=>{"user_id"=>"3", "result"=>"W"}, "1"=>{"user_id"=>"2", "result"=>"W"}, "2"=>{"user_id"=>"1", "result"=>"W"}, "3"=>{"user_id"=>"6", "result"=>"W"}}, "winning_score"=>"18", "losing_score"=>"4", "notes"=>"13241234"}, "commit"=>"Submit!"}
   (0.1ms)  begin transaction
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 6], ["LIMIT", 1]]
   (0.1ms)  rollback transaction
  Rendering games/new.html.erb within layouts/application
  Rendered games/new.html.erb within layouts/application (69.4ms)
  Rendered layouts/_shim.html.erb (0.4ms)
  Rendered layouts/_header.html.erb (0.7ms)
  Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 199ms (Views: 144.9ms | ActiveRecord: 0.5ms)

1 个答案:

答案 0 :(得分:0)

@kkulikovskis评论为我工作。我改变了:

has_many :participants

has_many :participants, inverse_of: :game

在游戏控制器中