在Rails应用程序中,我正在团队和玩家之间创建has_many关联,可以从控制台保存数据,但是从浏览器中不能保存数据。这些值将回滚并在日志中显示head no content
错误。
很明显,将值从new.html.erb传递给PlayersContoller时出错。我在玩家模型中没有任何验证。我是一个初学者,每个建议都是瑰宝,请为我提供宝贵的指导
// routes.rb
Rails.application.routes.draw do
resources :teams do
collection do
get :load_users
end
end
devise_for :users, controllers: { registrations: 'users/registrations' }
resources :user_roles
resources :roles
resources :users
resources :players
root 'home#home'
end
//服务器日志
Started GET "/players/new?team_id=11" for 127.0.0.1 at 2019-02-28 05:11:28 +0530
Processing by PlayersController#new as HTML
Parameters: {"team_id"=>"11"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ /home/sushmitha/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Team Load (0.2ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = ? LIMIT ? [["id", 11], ["LIMIT", 1]]
↳ app/controllers/players_controller.rb:4
Rendering players/new.html.erb within layouts/application
User Load (0.5ms) SELECT "users".* FROM "users" LIMIT ? OFFSET ? [["LIMIT", 2], ["OFFSET", 0]]
↳ app/views/players/new.html.erb:15
(0.3ms) SELECT COUNT(*) FROM "users"
↳ app/views/players/new.html.erb:34
/home/sushmitha/.rvm/gems/ruby-2.5.1/gems/will_paginate-bootstrap-1.0.1/lib/bootstrap_pagination/bootstrap_renderer.rb:11: warning: constant ::Fixnum is deprecated
Rendered players/new.html.erb within layouts/application (71.0ms)
Rendered layouts/_header.html.erb (0.9ms)
Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 162ms (Views: 118.0ms | ActiveRecord: 2.7ms)
Started POST "/players" for 127.0.0.1 at 2019-02-28 05:11:31 +0530
Processing by PlayersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"kyrK8LmWKA8E8lBsbQTVi2nVdjCvUkbm/tRjk7aLnRy8TlK93E1bBscmVsG//LC5JrxDBdVvyVMybIGTsf2+vQ==", "player"=>{"team_id"=>"11"}, "players"=>{"user_id"=>["1"]}, "commit"=>"Create Player"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ /home/sushmitha/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Team Load (0.2ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = ? LIMIT ? [["id", 11], ["LIMIT", 1]]
↳ app/controllers/players_controller.rb:14
(0.1ms) begin transaction
↳ app/controllers/players_controller.rb:16
(0.1ms) rollback transaction
↳ app/controllers/players_controller.rb:16
No template found for PlayersController#create, rendering head :no_content
Completed 204 No Content in 79ms (ActiveRecord: 0.8ms)
// user.rb
class User < ApplicationRecord
has_many :user_roles
has_many :roles, through: :user_roles, :dependent => :destroy
# belongs_to :team ,optional: true, foreign_key: :captain_id
end
// player.rb
class Player < ApplicationRecord
belongs_to :team
belongs_to :user
end
// team.rb
class Team < ApplicationRecord
has_many :users, dependent: :destroy
belongs_to :captain, class_name: 'User'
has_many :players, dependent: :destroy
validates :ground_name, presence:true
validates :team_name, presence:true
validates :captain_id, presence:true, uniqueness:true
end
// new.html.erb
<div class="col-md-6 col-md-offset-3">
<div class="container">
<br>
<h3>Add players to team <%= @team.team_name %></h3>
<%= form_for @player do |f| %>
<table>
<tbody>
<%= f.hidden_field :team_id, value: @team.id %>
<tr>
<th>Player Id</th>
<th>Player Name</th>
<th>Select</th>
</tr>
<% @user.each do |x| %>
<tr>
<td><%= x.id %></td>
<td><%= x.full_name %></td>
<td><%= f.check_box_tag 'players[user_id][]',x.id %></td>
</tr>
<% end %>
<tr>
<td>
<%= f.submit nil, class: 'btn btn-primary' %>
<%= link_to t('.cancel', default:t("helpers.links.cancel")),
root_path, class:'btn btn-default' %>
</td>
</tr>
</tbody>
</table>
<% end %>
</div>
</div>
<%= will_paginate @user,renderer: BootstrapPagination::Rails, previous_label:"old",next_label: "new", inner_window:1, outer_window:1 %>
//播放器控制器
class PlayersController < ApplicationController
def new
@player= Player.new
@team = Team.find(params[:team_id])
@user = User.all.paginate(page: params[:page], per_page:2)
end
def create
@team = Team.find(params[:player][:team_id])
params[:player].each do |user_id|
@team.players.create(user_id: params[:user_id])
end
end
private
def player_params
params.require(:player).permit(:team_id,:user_id)
end
end
表单中存在一个循环,该循环提供了user_id。如何从用户模型中提取用户信息并将其分配给表单,实际上使我感到困惑。
答案 0 :(得分:1)
让我尝试解释它应该如何。您有一个播放器模型,可以帮助您关联用户和团队。这意味着它应该是:
function getExtension ($mime_type){
$extensions = array('image/jpeg' => 'jpeg',
'text/xml' => 'xml');
// Add as many other Mime Types / File Extensions as you like
return $extensions[$mime_type];
}
您在路由或PlayersController中不需要额外的class Team < ApplicationRecord
has_many :players, dependent: :destroy
has_many :users, through: :players
end
class User < ApplicationRecord
has_many :players, dependent: :destroy
has_many :teams, through: :players
end
行。您只需要更新一个团队即可添加玩家。如果要在单独的视图中执行此操作,请在TeamsController中创建add_players操作。我将为您提供一个TeamsController的标准new-create / edit-update操作的视图示例,我想您已经有了
resources :players
还应该在控制器<%= form_for @team do |f| %>
# here goes all other team fields
<h3>Add players to team <%= @team.team_name %></h3>
<table>
<tbody>
<tr>
<th>Player Id</th>
<th>Player Name</th>
<th>Select</th>
</tr>
<% User.all.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.full_name %></td>
<td><%= check_box_tag 'team[user_ids][]', user.id %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit "Save", class: 'btn btn-primary' %>
<%= link_to t('.cancel', default:t("helpers.links.cancel")),
root_path, class:'btn btn-default' %>
<% end %>
中添加user_ids: []
答案 1 :(得分:0)
我试图遍历符号,这就是循环错误
react-native
使用以下代码对netw.html.erb进行了更改
def create
@team = Team.find(params[:player][:team_id])
params[:users].each do |user|
@team.players.create(user_id: user)
end
end