RoR custom routing/Method/View problem all methods come back as undefined
Posted
by
Jeff
on Stack Overflow
See other posts from Stack Overflow
or by Jeff
Published on 2011-01-05T22:43:59Z
Indexed on
2011/01/05
22:53 UTC
Read the original article
Hit count: 190
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.
© Stack Overflow or respective owner