1

我正在玩自定义视图和路线。我认为我做的一切都是对的,但显然不是。本质上我试图复制 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"}
4

2 回答 2

2

路线根据它们在您的路线文件中出现的顺序被考虑在内。

我猜127.0.0.1:/father/1/ofmine被解释为resources :fathers

放在match 'hospitals/:id/ofmine' => 'father#show2'您的 routes.rb 顶部尝试

编辑1:

我想,你犯了一个错误:

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

为了有一个更干净的文件,我会这样做:

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

编辑2:

你的控制器中有一个 show2 方法来获取变量吗?

我在想,你假设我当前的方法处理了错误的情况

于 2011-01-05T22:49:26.590 回答
0

当您尝试访问@father的名称时会出现错误。问题是它@father是空的。

我注意到的另一件事是您的网址应该是复数形式,例如/fathers/1. rake routes从命令行运行以查看您的路线的外观。

于 2011-01-05T23:04:13.047 回答