微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – Rails路由无法使用资源:模型

在我的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动作就像路线那样?到目前为止,我所有的其他模型都使用“资源:模型”指令工作得很好.

解决方法

在服务器输出中我们有这个:
由ChildrenController处理#index为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方法)?

只是为了说清楚,这不是路由错误,是模板错误

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐