0
不知何故,我的创建操作无法正常工作,但编辑操作正常。我的代码中某处出错了,我似乎无法找到它。正如标题所说,即使填写我的字段,我也会创建一个空白条目。嵌套对象创建空白条目
花名册嵌套在团队类中,像这样。
/团队/ ID /名册/ ID
rosters_controller.rb
class RostersController < ApplicationController
before_action :set_roster, only: [:show, :edit, :update, :destroy, :player_roster]
before_action :team
before_filter :authenticate_user!
def index
@rosters = @team.rosters
end
def show
@players = Player.all
end
def new
@roster = Roster.new
end
def edit
@players = Player.all
end
def create
@roster = @team.rosters.build(params[:roster_params])
respond_to do |format|
if @roster.save
format.html { redirect_to team_rosters_path(@roster.team), notice: 'Roster was successfully created.' }
format.json { render action: 'show', status: :created, location: @roster.team }
else
format.html { render action: 'new' }
format.json { render json: @roster.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @roster.update(roster_params)
format.html { redirect_to team_rosters_path(@roster.team), notice: 'Roster was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @roster.errors, status: :unprocessable_entity }
end
end
end
def destroy
@roster.destroy
respond_to do |format|
format.html { redirect_to team_rosters_path(@roster.team), notice: 'Roster was successfully deleted.' }
format.json { head :no_content }
end
end
private
def team
@team = Team.find(params[:team_id])
end
def set_roster
@roster = Roster.find(params[:id])
end
def roster_params
params.require(:roster).permit(:name, :team_id, :starts_at, :ends_at, :current, :player_tokens, :q)
end
end
名册形成
<%= form_for([@team, @roster]) do |f| %>
<% if @roster.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@roster.errors.count, "error") %> prohibited this roster from being saved:</h2>
<ul>
<% @roster.errors.full_messages.each do |msg| %><li><%= msg %></li><% end %>
</ul>
</div>
<% end %>
<%= f.label :current %><br>
<%= f.check_box :current %><br>
<%= f.label :starts_at %><br>
<%= f.date_select :starts_at %><br>
<%= f.label :ends_at %><br>
<%= f.date_select :ends_at, {:include_blank => true, :default => nil} %><br>
<%= f.label :player_tokens, "Players" %><br />
<%= f.text_field :player_tokens, "data-pre" => @roster.players.map(&:attributes).to_json, :id => "player-tokens" %><br>
<%= f.submit :class => "btn btn-warning" %>
<script type="text/javascript">
$(document).ready(function() {
$("#player-tokens").tokenInput("/players.json", {
crossDomain: false,
prePopulate: $("#roster_player_tokens").data("pre"),
theme: "facebook",
propertyToSearch: "gamertag",
hintText: "Type in a gamertag",
searchingText: "Searching...",
tokenLimit: 4,
preventDuplicates: true
});
});
</script>
<% end %>
名册模型
class Roster < ActiveRecord::Base
attr_reader :player_tokens
has_and_belongs_to_many :players
has_many :placements
belongs_to :team, touch: true
def current_roster?
current?
end
def player_tokens=(ids)
self.player_ids = ids.split(",")
end
def self.total_prize_money
sum.placements(:prize_money)
end
end
请分享你的团队的模型,以及 – Babar