【问题标题】:Rails route not working using resource :modelRails 路线无法使用资源:model
【发布时间】:2011-11-07 18:19:13
【问题描述】:

在我的 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"}

这是我的 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

当我访问网址“/children”时,我收到此错误:

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”指令运行良好。

【问题讨论】:

  • 你能发布你的整个路线文件吗?可能有干扰
  • 你能提供link_to show所在的代码块吗?
  • 您可以编辑您的问题以添加rake routes 的输出吗?
  • 只有在路由有一个单一的映射资源 :children 时才会寻找 GET /children url 的 show 动作。
  • 对不起,不是完整的跟踪,来自服务器的输出。 “无路由匹配”的控制台输出

标签: ruby-on-rails ruby routes models


【解决方案1】:

在服务器输出中我们有这个: Processing by ChildrenController#index as 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 行你遇到了问题,可能在这里:child_path(@child)

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


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

【讨论】:

  • 啊,我明白了。我对 Rails 有点太陌生了,不知道每个地方都可以看,但是整个问题只是为我提供了未来故障排除的途径。我想我可以从这里弄清楚该怎么做。
  • 别担心,学习的时候犯错是可以的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
相关资源
最近更新 更多