Rendering one models 'edit form' in anothers 'index view'

I have two models, Teams and Players. On the teams index page I have a list of players that aren't assigned to a team. I'm trying to make a button so that I can click on one of the players with no team and have the 'edit form' of this player show up on the team index page.

This is my current team#index:

= link_to 'New Team', new_team_path
= link_to 'New Player', new_player_path

  - @teams.each do |team|
          = link_to truncate(, length: 18), edit_team_path(team)
          = number_to_currency(team.adjust_money, precision: 0)
          %th.namecolumn Player
          %th.poscolumn Pos
          %th.pricecolumn $
        -team.players.each do |player|
            %td.poscolumn= player.position
            %td.pricecolumn= player.price
        -(1..(10-team.players.length)).each do |x|
            %td ---
=render template: 'players/edit'
=render 'players/playerlist'

and this is my player#edit

%h1 Nominated Player
= render 'players/form'

= link_to 'Show', @player
= link_to 'Back', players_path

and the players/form

  <%= form_for(@player) do |f| %>
     <% if @player.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@player.errors.count, "error") %> prohibited this player from being saved:</h2>
        <% @player.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
    <% end %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    <div class="field">
      <%= f.label :position %><br />
      <%= f.text_field :position %>
    <div class="field">
      <%= f.label :price %><br />
      <%= f.number_field :price %>
    <div class="field">
      <%= f.label :team_id %><br />
      <%= :team_id, { |team| [,] }, { :include_blank =>     true } %>
    <div class="actions">
      <%= f.submit %>
  <% end %>

At the moment I get this error 'undefined method `model_name' for NilClass:Class' I think its because the form doesn't have access to @player which is defined in the players edit action. Is there a way I can get this to work somehow?


You can reference any partial from another view page, and that's fine. However, like in your case, if that partial you require needs some instance variables (like @player) you'll have to either: A) declare it in the controller of Teams, or B) pass it in to the partial.

So for A), in your Teams controller for action index, just add @player = or whatever you need it to be.

For B), do:

render :partial => "my_partial", :locals => {:player =>}

Need Your Help

IIRF Regex for Domain-based rewrites

regex iirf

I have seen a ton of examples for URL rewrites based on the subdomain, but I am trying to do URL rewrites based on the domain name itself, specifically serve content from a subfolder with the domai...