1

I am playing with custom view and routes. I think that I have everything right but obviously not. Essentially I tried to copy the show method and show.html.erb but for some reason it will not work.

My controller

    class fatherController < ApplicationController
      def show
        @father = Father.find(params[:id])

        respond_to do |format|
         format.html # show.html.erb
         format.xml  { render :xml => @father }
        end
      end

      def ofmine
        @father = Father.find(params[:id])

        respond_to do |format|
          format.html # show.html.erb
          format.xml  { render :xml => @father }
        end
      end
   end

My routes.rb

Parent::Application.routes.draw do
  resources :fathers do
     resources :kids
  end 

  match 'hospitals/:id/ofmine' => 'father#show2'
end

when I go to

127.0.0.1:/father/1

it works fine but when I try to go to

127.0.0.1:/father/1/ofmine

it gives the following error. It doesn't matter what the variable/method that is called; it occurs at the first one to be displayed. Both show.html.erb and show2.html.erb are the exact same files

My Error from webserver commandline

> Processing by fathersController#show2
> as HTML   Parameters: {"id"=>"1"}
> Rendered fathers/show2.html.erb within
> layouts/application (31.6ms) Completed
> in 37ms
> 
> ActionView::Template::Error (undefined
> method `name' for nil:NilClass):
>     4:         <td>Name</td><td></td>
>     5:     </tr>
>     6:     <tr>
>     7:  <td><%= @father.name %></td><td></td>
>     8:     </tr>
>     9:     <tr>
>     10:  <td>City</td><td>State</td>   app/views/fathers/show2.html.erb:7:in
> `_app_views_fatherss_show__html_erb___709193087__616989688_0'

Error as displayed on actual page

NoMethodError in Fathers#show2

Showing /var/ruby/chs/app/views/fathers/show2.html.erb where line #7 raised:

undefined method `name' for nil:NilClass

Extracted source (around line #7):

4: Name 5:
6: 7: <%= @father.name %> 8:
9: 10: CityState

If anyone could tell me what in the world I am doing wrong I would appreciate it greatly.

Here is a copy of my rake routes

     father_ofmine      /fathers/:father_id/ofmine(.:format)               {:action=>"show2", :controller=>"fathers"}
     father_kids GET    /fathers/:father_id/kids(.:format)          {:action=>"index", :controller=>"kids"}
                 POST   /fathers/:father_id/kids(.:format)          {:action=>"create", :controller=>"kids"}
  new_father_kid GET    /fathers/:father_id/kids/new(.:format)      {:action=>"new", :controller=>"kids"}
 edit_father_kid GET    /fathers/:father_id/kids/:id/edit(.:format) {:action=>"edit", :controller=>"kids"}
      father_kid GET    /fathers/:father_id/kids/:id(.:format)      {:action=>"show", :controller=>"kids"}
                 PUT    /fathers/:father_id/kids/:id(.:format)      {:action=>"update", :controller=>"kids"}
                 DELETE /fathers/:father_id/kids/:id(.:format)      {:action=>"destroy", :controller=>"kids"}
         fathers GET    /fathers(.:format)                                     {:action=>"index", :controller=>"fathers"}
                 POST   /fathers(.:format)                                     {:action=>"create", :controller=>"fathers"}
      new_father GET    /fathers/new(.:format)                                 {:action=>"new", :controller=>"fathers"}
     edit_father GET    /fathers/:id/edit(.:format)                            {:action=>"edit", :controller=>"fathers"}
          father GET    /fathers/:id(.:format)                                 {:action=>"show", :controller=>"fathers"}
                 PUT    /fathers/:id(.:format)                                 {:action=>"update", :controller=>"fathers"}
                 DELETE /fathers/:id(.:format)                                 {:action=>"destroy", :controller=>"fathers"}
Jeff
  • 13
  • 4

2 Answers2

2

Routes are taken into account according to their order of appearance in your routes file.

I guess 127.0.0.1:/father/1/ofmine is interpreted as part of resources :fathers

Put match 'hospitals/:id/ofmine' => 'father#show2' at the top of your routes.rb to try

EDIT 1:

I guess, you made a mistake:

# instead of match 'hospitals/:id/ofmine' => 'father#show2' 
match 'father/:id/ofmine' => 'father#show2'

And to have a cleaner file, I'd do:

Parent::Application.routes.draw do
  resources :fathers do
    match '/ofmine' => 'father#show2'
    resources :kids
  end 
end

EDIT 2:

Do you have a show2 method in your controller which gets the variable?

I'm thinking, you're assuming the current ofmine method handles the situation which is wrong

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • Ahaa, now I see. So that `match 'father/:id/ofmine' => 'father#show2'` route makes `show2` view render even if there's no action for it. I tested it quickly and it seems to work like that. Without action it's no wonder if `@father` is null. – Heikki Jan 05 '11 at 23:14
  • right, there is no need to put a method in the controller for the view to be displayed :) – apneadiving Jan 05 '11 at 23:17
  • I have tried it both ways ':controller/:action/:id' and ':controller/:id/:action' and it still doesn't work. are you saying that I should rename my method of mine to show2? – Jeff Jan 05 '11 at 23:18
  • right :) '=> 'father#show2' means: go to controller 'father' and fetch method 'show2' – apneadiving Jan 05 '11 at 23:20
  • well looky there. it was a little more complicated. I had to make my method match my view file and add a route for it as well. Thank you so much for the help. – Jeff Jan 05 '11 at 23:28
  • @apneadiving +1 for pointing out that the order of the statements in the routes file matters – Bnjmn Mar 02 '13 at 05:50
0

The error shows up when you try to access @father's name. The problem is that @father is null.

Another thing I noticed is that your urls should be in plural, like /fathers/1. Run rake routes from the command line to see how your routes look like.

Heikki
  • 15,329
  • 2
  • 54
  • 49