我正在玩自定义视图和路线。我认为我做的一切都是对的,但显然不是。本质上我试图复制 show 方法和 show.html.erb 但由于某种原因它不起作用。
我的控制器
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
我的路线.rb
Parent::Application.routes.draw do
resources :fathers do
resources :kids
end
match 'hospitals/:id/ofmine' => 'father#show2'
end
当我去
127.0.0.1:/父亲/1
它工作正常,但当我尝试去
127.0.0.1:/father/1/ofmine
它给出了以下错误。调用什么变量/方法并不重要;它出现在第一个要显示的位置。show.html.erb 和 show2.html.erb 都是完全相同的文件
我的网络服务器命令行错误
> 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'
实际页面显示的错误
父亲中的 NoMethodError #show2
显示 /var/ruby/chs/app/views/fathers/show2.html.erb 其中第 7 行提出:
nil:NilClass 的未定义方法“名称”
提取的源代码(第 7 行附近):
4: 姓名 5:
6: 7: <%= @father.name %> 8:
9: 10: CityState
如果有人能告诉我世界上我做错了什么,我将不胜感激。
这是我的 rake 路线的副本
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"}