2011-11-07 80 views
5

在我的Rails应用程序3.1,我有一个模型命名为“孩子”Rails的路线不工作:模型

在我的routes.rb文件,我也行:

resources :children 

这里整个routes.rb中文字:

root :to => "pages#home" 

    resources :children 

以下是完整的耙路线的结果(注意,大多数航线都有相关ActiveAdmin):

    children GET  /children(.:format)      {:action=>"index", :controller=>"children"} 
          POST  /children(.:format)      {:action=>"create", :controller=>"children"} 
       new_child GET  /children/new(.:format)     {:action=>"new", :controller=>"children"} 
       edit_child GET  /children/:id/edit(.:format)    {:action=>"edit", :controller=>"children"} 
        child GET  /children/:id(.:format)     {:action=>"show", :controller=>"children"} 
          PUT  /children/:id(.:format)     {:action=>"update", :controller=>"children"} 
          DELETE  /children/:id(.:format)     {:action=>"destroy", :controller=>"children"} 

当我运行 “耙路线:” 我看到这行的结果:

children GET /children(.:format) {:action=>"index", :controller=>"children"} 

这是我ChildrenController代码:

 def index 
     @children = Child.all 

     @base_class = "children-index" 
     @title = "Your Children" 

     respond_to do |format| 
      format.html # children/index.html.erb 
      format.json { render :json => @children } 
     end 
    end 

    def show 
     @child = Child.find(params[:id]) 

     @base_class = "child-show" 
     @title = child_name(@child) 

     respond_to do |format| 
      format.html # children/show.html.erb 
      format.json { render :json => @child } 
     end 
    end 

当我访问的URL“/儿童“我得到这个错误:

No route matches {:action=>"show", :controller=>"children"} 

下面是完整的跟踪:

Started GET "/children" for 127.0.0.1 at 2011-11-07 13:06:24 -0600 
    Processing by ChildrenController#index as HTML 
    Child Load (0.8ms) SELECT `children`.* FROM `children` 
Rendered children/index.html.erb within layouts/application (65.0ms) 
Completed 500 Internal Server Error in 166ms 

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"children"}): 
    1: <h1><%= title %></h1> 
    2: <ul> 
    3: <%= @children.each do |child| %> 
    4:  <li><%= link_to child.child_name(child), child_path(@child) %></li> 
    5: <% end %> 
    6: </ul> 
    app/views/children/index.html.erb:4:in `block in _app_views_children_index_html_erb__674498165009231817_70298485459960' 
    app/views/children/index.html.erb:3:in `each' 
    app/views/children/index.html.erb:3:in `_app_views_children_index_html_erb__674498165009231817_70298485459960' 
    app/controllers/children_controller.rb:9:in `index' 

Rendered /.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms) 

为什么“/ children”试图执行“show”动作,以及为什么show动作像路由不在那里一样?我所有的其他模型都使用“resource:model”指令工作得很好。

+0

你可以发布你的整个路径文件?可能有些干扰 –

+0

你能提供大量的代码,其中link_to显示是? – ck3g

+0

你可以编辑你的问题来添加'rake routes'的输出吗? –

回答

5

在服务器输出我们有这个: Processing by ChildrenController#index as HTML 确认路线是正确的。

但在您的模板中,您正在使用服务器找不到的路线。

::的ActionView ::模板错误(没有路由匹配{:动作=> “秀”,:控制器=> “孩子”}):

1: <h1><%= title %></h1> 
2: <ul> 
3: <%= @children.each do |child| %> 
4:  <li><%= link_to child.child_name(child), child_path(@child) %></li> 
5: <% end %> 
6: </ul> 

更确切地说,在第4行你有问题,大概在这里:child_path(@child)

你使用模型的任何自定义ID(覆盖to_param方法)?


只是要清楚,这不是一个路由错误,是一个模板错误

+0

啊,我明白了。我对Rails有点太小了,以便知道每个地方的样子,但这整个问题给了我一个未来故障排除的途径。我想我可以从这里弄清楚该怎么做。 – wrburgess

+1

别担心,你学习的时候可以犯错误。 –