Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails路由无法使用资源:模型_Ruby On Rails_Ruby_Routes_Models - Fatal编程技术网

Ruby on rails Rails路由无法使用资源:模型

Ruby on rails Rails路由无法使用资源:模型,ruby-on-rails,ruby,routes,models,Ruby On Rails,Ruby,Routes,Models,在我的Rails 3.1应用程序中,我有一个名为Child的模型 在我的routes.rb文件中,我有一行: resources :children 以下是routes.rb全文: root :to => "pages#home" resources :children 以下是完整的rake路由结果。请注意,大多数路由与ActiveAdmin相关: children GET /children(.:format)

在我的Rails 3.1应用程序中,我有一个名为Child的模型

在我的routes.rb文件中,我有一行:

resources :children
以下是routes.rb全文:

  root :to => "pages#home"

  resources :children
以下是完整的rake路由结果。请注意,大多数路由与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"}
当我运行rake routes时,我会在结果中看到这一行:

children GET  /children(.:format) {:action=>"index", :controller=>"children"}
这是我的孩子控制器中的代码:

     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指令,我的所有其他模型都运行良好。

在服务器输出中,我们有以下内容: ChildrenControllerindex以HTML格式处理 这证实了路线是正确的

但在模板中,您使用的是服务器找不到的路由

ActionView::Template::Error没有路由匹配{: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>
更准确地说,在第4行中,您遇到了一个问题,可能在这里:孩子_path@child

您是否为覆盖to_param方法的模型使用任何自定义ID


请澄清,这不是路由错误,是模板错误

您可以发布整个路由文件吗?可能存在干扰。您可以提供要显示的链接的代码块吗?您可以编辑您的问题以添加rake路由的输出吗?如果路由具有单一映射资源:children,则只会查找GET/children url的显示操作。抱歉,不是完整跟踪,而是服务器的输出。控制台输出来自无路由匹配Sah,我理解。我对Rails有点太无知了,不知道每个地方该看什么,但整个问题只是给了我一条未来故障排除的道路。我想我可以从这里弄明白该怎么做。别担心,当你在学习的时候犯错误是可以的。