RoR custom routing/Method/View problem all methods come back as undefined
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开发者_如何学Python 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"}
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
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.
精彩评论