我的创建操作无法正常工作,但编辑操作正常。在我的代码中某处出了点问题,我似乎无法找到它。正如标题所说,即使填写我的字段,我也会创建一个空白条目。
Roster嵌套在团队类中,就像这样。
/支/ 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
答案 0 :(得分:0)
您不在create
方法中使用强参数,因此所有参数都不会传递给您的模型。该模型没有对属性的存在进行任何验证,因此它只会创建一个空白记录。
在控制器中,您需要更改为使用已在更新操作中使用的强参数。变化:
def create
@roster = @team.rosters.build(params[:roster_params])
...
到:
def create
@roster = @team.rosters.build(roster_params)
...