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

#teamLists
  - @teams.each do |team|
    .team
      .teamtitle
        .teamname
          = link_to truncate(team.name, length: 18), edit_team_path(team)
        .teammoney
          = number_to_currency(team.adjust_money, precision: 0)
      %table
        %tr.tableheading
          %th.namecolumn Player
          %th.poscolumn Pos
          %th.pricecolumn $
        -team.players.each do |player|
          %tr
            %td.namecolumn= player.name
            %td.poscolumn= player.position
            %td.pricecolumn= player.price
        -(1..(10-team.players.length)).each do |x|
          %tr
            %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>
        <ul>
        <% @player.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
         </ul>
      </div>
    <% end %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    </div>
    <div class="field">
      <%= f.label :position %><br />
      <%= f.text_field :position %>
    </div>
    <div class="field">
      <%= f.label :price %><br />
      <%= f.number_field :price %>
    </div>
    <div class="field">
      <%= f.label :team_id %><br />
      <%= f.select :team_id, Team.all.map { |team| [team.name, team.id] }, { :include_blank =>     true } %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% 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?

Answers


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 = Player.new or whatever you need it to be.

For B), do:

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


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...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.