Forum: Ruby on Rails Very odd RoR behavior. Something is wrong here.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adam Klunick (Guest)
on 2007-06-05 01:24
(Received via mailing list)
I have 2 different actions for a single controller (show and
shirt_report).  Each action has its own view.  I have included the
code for each below.  The problem is that I am receiving an error when
generating shirt_report, but not show, on my webserver running in
production, but not on my laptop running in development.  The error
recieved is also listed below.  Any help in clearing up this issue
would be greatly appreciated.

================
Controller:

class TeamController < ApplicationController

  def show
    @team = current_user.teams.find(params[:id])
  end

  def shirt_report
    @team = current_user.teams.find(params[:id])
    @players = @team.players.find(:all, :order => 'last_name ASC')
  end

end

================

View: show.rhtml

<h1><%= @team.name %></h1>
<% if !@team.players.empty? %>
  <h3>Roster</h3>
  <%= link_to ' (Shirt Report)',
              :action => 'shirt_report',
              :id => @team %>
  <div class="indent">
  <table>
    <thead><td>Player Name</td><td>Height</td><td>Weight</td></thead>
    <tbody>
    <% @team.players.each do |player| %>
      <tr>
        <td>
          <%= link_to h(player.last_name) + ', ' +
h(player.first_name),
                      :controller => 'player',
                      :action => 'show',
                      :id => player %>
        </td>
        <td>
          <%= h player.height %>
        </td>
        <td>
          <%= h player.weight %>
        </td>
      </tr>
    <% end %>
    </tbody>
  </table>
  </div>
<% end %>

================

View: shirt_report.rhtml

<h3><%= @team.name %> Shirt Report</h3>
<div class="indent">
<table>
  <thead><td>Player Name</td><td>Shirt Size</td></thead>
  <tbody>
  <% @players.each do |player| %>
    <tr>
      <td>
        <%= link_to h(player.last_name) + ', ' + h(player.first_name),
                    :controller => 'player',
                    :action => 'show',
                    :id => player %>
      </td>
      <td>
        <%= h player.shirt_size %>
      </td>
    </tr>
  <% end %>
  </tbody>
</table>
</div>

================

The error when clicking on one of the links generated in show to get
to the shirt_report:

ActionView::TemplateError (undefined method `name' for nil:NilClass)
on line #1 of app/views/team/shirt_report.rhtml:
1: <h3><%= @team.name %> Shirt Report</h3>
2: <div class="indent">
3: <table>
4:   <thead><td>Player Name</td><td>Shirt Size</td></thead>

    #{RAILS_ROOT}/app/views/team/shirt_report.rhtml:1:in
`_run_rhtml_47app47views47team47shirt_report46rhtml'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_view/base.rb:326:in `send'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_view/base.rb:326:in `compile_and_render_template'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_view/base.rb:301:in `render_template'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_view/base.rb:260:in `render_file'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/base.rb:806:in `render_file'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/base.rb:711:in `render_with_no_layout'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/layout.rb:247:in `render_without_benchmark'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/benchmarking.rb:50:in `render'
    /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/benchmarking.rb:50:in `render'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/base.rb:1101:in `perform_action_without_filters'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/filters.rb:632:in `call_filter'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/filters.rb:638:in `call_filter'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/filters.rb:438:in `call'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/filters.rb:637:in `call_filter'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/filters.rb:619:in `perform_action_without_benchmark'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
    /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/rescue.rb:83:in `perform_action'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/base.rb:430:in `send'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/base.rb:430:in `process_without_filters'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/filters.rb:624:in
`process_without_session_management_support'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/session_management.rb:114:in `process'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_controller/base.rb:330:in `process'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:
41:in `dispatch'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/fcgi_handler.rb:
168:in `process_request'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/fcgi_handler.rb:
143:in `process_each_request!'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/fcgi_handler.rb:
109:in `with_signal_handler'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/fcgi_handler.rb:
142:in `process_each_request!'
    /usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.7/lib/fcgi.rb:612:in
`each_cgi'
    /usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.7/lib/fcgi.rb:609:in
`each'
    /usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.7/lib/fcgi.rb:609:in
`each_cgi'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/fcgi_handler.rb:
141:in `process_each_request!'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/fcgi_handler.rb:
55:in `process!'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/fcgi_handler.rb:
25:in `process!'
    dispatch.fcgi:24

================

-Adam Klunick
Richard L. (Guest)
on 2007-06-05 01:35
(Received via mailing list)
Production doesnt have the same 'team' data that development has?
 - Richard
Adam Klunick (Guest)
on 2007-06-05 01:48
(Received via mailing list)
If you notice, there is a reference to @team.name in both show and
shirt_report.  Show works but shirt_report does not.
John T. (Guest)
on 2007-06-05 02:24
(Received via mailing list)
Hi,
In your show.rhtml you have:

View: show.rhtml

<h1><%= @team.name %></h1>
<% if !removed_email_address@domain.invalid? %>
  <h3>Roster</h3>
  <%= link_to ' (Shirt Report)',
              :action => 'shirt_report',
              :id => @team %>
  <div class="indent">
  <table>
...
...

Your link_to you have :id => @team, which should really be @team.id,
otherwise you're trying to pass the object @team, and not the id value
for it.

John.
Rick O. (Guest)
on 2007-06-05 02:53
(Received via mailing list)
On 6/4/07, John <removed_email_address@domain.invalid> wrote:
>               :action => 'shirt_report',
> John.
Routes call an implicit #to_param method on values, which is mapped to
#id on AR models by default.  His code is fine, and has nothing to do
with the error.

--
Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
Rob B. (Guest)
on 2007-06-05 03:09
(Received via mailing list)
On Jun 4, 2007, at 5:24 PM, Adam Klunick wrote:

> Controller:
>   end
>   <%= link_to ' (Shirt Report)',
> h(player.first_name),
>                       :controller => 'player',
>                       :action => 'show',
>                       :id => player %>
Are you talking about THIS link?
Because /player/show/:id seems to expect that the :id is for a TEAM,
not a player.

And the use of
:id => some_object
calls the .to_param method of some_object which, by default, does .id

Perhaps you meant for for your controller action to be:
   def show
     @team = current_user.teams.find(params[:id])
     @player = @team.players.find(params[:player])
   end

and in the view:
    <%= link_to player.full_name,
                       :controller => 'player',
                       :action => 'show',
                       :id => @team,
                       :player => player %>

And I'd suggest:
class Player
   def roster_name
     "#{self.last_name}, #{self.first_name}"
   end
end

>   </table>
>   <thead><td>Player Name</td><td>Shirt Size</td></thead>
>         <%= h player.shirt_size %>
>       </td>
>     </tr>
>   <% end %>
>   </tbody>
> </table>
> </div>
>
> ================
>
> -Adam Klunick

-Rob

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Rob B. (Guest)
on 2007-06-05 03:13
(Received via mailing list)
OR, should you show us the PlayerController#show as well?

On Jun 4, 2007, at 7:08 PM, Rob B. wrote:

>> recieved is also listed below.  Any help in clearing up this issue
>>
>>
>>     <% @team.players.each do |player| %>
>
> and in the view:
>   end
>>     <% end %>
>> <div class="indent">
>>                     :id => player %>
>> ================
>>
>> -Adam Klunick
>
> -Rob
>
> Rob B.    http://agileconsultingllc.com
> removed_email_address@domain.invalid
>
>

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
+1 513-295-4739
Skype:  rob.biedenharn
This topic is locked and can not be replied to.