title: NoMethodError in Teams#edit with undefined method `model_name' for NilClass:Class in ruby on rails application
body: <p>I know this kind of question is answered many times on this forum. But I tried a lot on stackoverflow and Google but could not solve this problem.</p>
  
  <p>In my project, I have team, user and team_user model which allow to create team and while creating new team, I can also add new users and team lead. Currently I am able to create team with users and team lead.There is many to many relationship between user and team with team_user is joining model.</p>
  
  <p><em>Problem</em>
  When I click on edit, it throws  "<strong><code>NoMethodError in Teams</code></strong>" error with <strong><code>"model_name' for NilClass:Class"</code></strong>. "Edit" action is available in team controller and which has <strong><code>@team</code></strong> instance variable and it is used in _form.html.erb(edit). I tried in many ways but could not find solution.</p>
  
  <p>*teams_controller.rb*</p>
  
  <pre><code>class TeamsController &lt; 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 =&gt; params[:users])
          users.each {|user| @team.users &lt;&lt; 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
  </code></pre>
  
  <p>*<strong>_form.html.erb(edit)*</strong></p>
  
  <pre><code> &lt;%= form_for @team do |f| %&gt;
  
    &lt;div style=" margin-top:10px"&gt;
      &lt;label&gt; Team Name &lt;/label&gt;
      &lt;%= f.text_field :name, :class =&gt; 'text_field' %&gt;
    &lt;/div&gt;
  
    &lt;label&gt; Add Users &lt;/label&gt;
    &lt;%= select_tag "users[]", options_from_collection_for_select( @users, :id, :first_name),:style =&gt; "width:270px; height:35px", :id =&gt; "drp_Books_Ill_Illustrations",
       :multiple =&gt; true %&gt;
  
     &lt;label&gt; Team Lead &lt;/label&gt;
    &lt;%= select_tag(:team_lead_id, options_from_collection_for_select(@users, :id, :first_name)) %&gt;
  
    &lt;div class=modal-footer&gt;
      &lt;button class="btn" data-dismiss="modal" aria-hidden="true"&gt;Cancel&lt;/button&gt;
      &lt;%= f.submit 'Create Team', :class =&gt; 'btn btn-primary' %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>*_table.html(edit)*</p>
  
  <pre><code>&lt;%= stylesheet_link_tag "team", :media =&gt; "all" %&gt;
  &lt;table  id='example'  class=" table-boarder roler "&gt;
    &lt;thead style="background-color: #ffffff"&gt;
      &lt;tr&gt;
        &lt;th &gt;Team Id &lt;/th&gt;
        &lt;th&gt; Team Name &lt;/th&gt;
        &lt;th&gt;Team Lead &lt;/th&gt;
        &lt;th&gt; Team users &lt;/th&gt;
        &lt;th&gt; Created at &lt;/th&gt;
        &lt;th&gt; Update at &lt;/th&gt;
        &lt;th&gt; Action&lt;/th&gt;
        &lt;th&gt; Action&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
      &lt;tbody class="table-hover"&gt;
        &lt;% @teams.each do |teams| %&gt;
          &lt;tr&gt;
            &lt;td&gt;&lt;%= teams.id %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%= teams.name %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%= teams.team_lead_id %&gt;&lt;/td&gt;
            &lt;td&gt; &lt;% teams.users.each do |users| %&gt;
            &lt;%= users.first_name %&gt;
            &lt;% end %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%= teams.created_at %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%= teams.updated_at %&gt;&lt;/td&gt;
             &lt;td&gt;
        &lt;%= link_to 'edit',edit_team_path(teams), :class =&gt; 'btn-small btn-primary' %&gt;
      &lt;/td&gt;
      &lt;td&gt;
         **&lt;%= link_to "delete", team_path(teams), :method =&gt; :delete, :class =&gt; 'btn-small btn-danger', :confirm =&gt; 'Are you sure to delete' %&gt;**
      &lt;/td&gt;
          &lt;/tr&gt;
       &lt;% end %&gt;
     &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Above is the table which has link for <strong><code>"delete".</code></strong>. I tried a lot with editing team but could not. </p>