0

我知道这种问题在这个论坛上被回答了很多次。但是我在 stackoverflow 和 Google 上尝试了很多,但无法解决这个问题。

在我的项目中,我有团队、用户和 team_user 模型,它们允许创建团队,在创建新团队的同时,我还可以添加新用户和团队负责人。目前,我能够与用户和团队领导一起创建团队。用户和团队之间存在多对多的关系,其中 team_user 正在加入模型。

问题 当我点击编辑时,它会抛出“ NoMethodError in Teams”错误"model_name' for NilClass:Class"。“编辑”操作在团队控制器中可用,它具有@team实例变量,并在 _form.html.erb(edit) 中使用。我尝试了很多方法,但找不到解决方案。

*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".。我与编辑团队进行了很多尝试,但不能。

4

1 回答 1

1

你有一个错字:

@team = Team.find(param[:id])

需要是

@team = Team.find(params[:id])

注意 params 中多余的 s

于 2013-06-29T22:54:19.473 回答