我知道在这个论坛上已经多次回答了这类问题。但我在stackoverflow和Google上做了很多尝试,但无法解决这个问题。
在我的项目中,我有团队,用户和team_user模型,可以创建团队,同时创建新团队,我还可以添加新用户和团队领导。目前我能够创建具有用户和团队领导的团队。用户和团队之间存在多对多的关系,team_user正在加入模型。
问题
当我点击修改时,会在 NoMethodError in Teams
时抛出“ "model_name' for NilClass:Class"
”错误。 “编辑”操作在团队控制器中可用,并且具有 @team
实例变量,并在_form.html.erb(编辑)中使用。我试过很多方法,但找不到解决办法。
* teams_controller.rb *
class TeamsController < ApplicationController
before_filter :authorize_admin!
before_filter :set_form_variables, only: [:new, :index] # everywhere the form is displayed
def index
@teams = Team.all
@team = Team.new
end
def new
@team = Team.new
@users = User.all
end
def edit
@team = Team.find(param[:id])
end
def create
@team = Team.new(params[:team])
team_lead = User.find(params[:team_lead_id])
@team.team_lead = team_lead
if @team.save
users = User.where(:id => params[:users])
users.each {|user| @team.users << user}
flash[:notice] = 'Team has been created'
redirect_to teams_path
else
flash[:alert] = 'Team not created'
redirect_to teams_path
end
end
private
def set_form_variables
@team = Team.new
@users = User.all
end
def update
puts "====================================="
@team = Team.find(params[:id])
if @team.update_attributes(params[:team])
flash.notice = "Team #{@team.name} has been updated"
redirect_to team_path
else
render 'edit'
end
end
def destroy
@team = Team.find(params[:id])
@team.destroy
redirect_to teams_path
end
end
*的 _form.html.erb(编辑)*
<%= form_for @team do |f| %>
<div style=" margin-top:10px">
<label> Team Name </label>
<%= f.text_field :name, :class => 'text_field' %>
</div>
<label> Add Users </label>
<%= select_tag "users[]", options_from_collection_for_select( @users, :id, :first_name),:style => "width:270px; height:35px", :id => "drp_Books_Ill_Illustrations",
:multiple => true %>
<label> Team Lead </label>
<%= select_tag(:team_lead_id, options_from_collection_for_select(@users, :id, :first_name)) %>
<div class=modal-footer>
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<%= f.submit 'Create Team', :class => 'btn btn-primary' %>
</div>
<% end %>
* _ table.html(编辑)*
<%= stylesheet_link_tag "team", :media => "all" %>
<table id='example' class=" table-boarder roler ">
<thead style="background-color: #ffffff">
<tr>
<th >Team Id </th>
<th> Team Name </th>
<th>Team Lead </th>
<th> Team users </th>
<th> Created at </th>
<th> Update at </th>
<th> Action</th>
<th> Action</th>
</tr>
</thead>
<tbody class="table-hover">
<% @teams.each do |teams| %>
<tr>
<td><%= teams.id %></td>
<td><%= teams.name %></td>
<td><%= teams.team_lead_id %></td>
<td> <% teams.users.each do |users| %>
<%= users.first_name %>
<% end %></td>
<td><%= teams.created_at %></td>
<td><%= teams.updated_at %></td>
<td>
<%= link_to 'edit',edit_team_path(teams), :class => 'btn-small btn-primary' %>
</td>
<td>
**<%= link_to "delete", team_path(teams), :method => :delete, :class => 'btn-small btn-danger', :confirm => 'Are you sure to delete' %>**
</td>
</tr>
<% end %>
</tbody>
</table>
以上是包含 "delete".
链接的表格。我和编辑团队做了很多尝试但是没有。
答案 0 :(得分:1)
你有一个错字:
@team = Team.find(param[:id])
需要
@team = Team.find(params[:id])
请注意参数中的额外 s 。