【问题标题】:Rails 3 - Missing Index Paths?Rails 3 - 缺少索引路径?
【发布时间】:2011-03-03 06:30:52
【问题描述】:

我的一个 Rails 应用程序出现了一个非常奇怪的问题。我想我可能正在做一些我无法识别的非常愚蠢的事情。我的问题是,我似乎丢失了大约一半的索引路径。

例如,如果我的控制器是模型 foo 的“foos”,我将拥有:

foos POST   /foos(.:format)      {:action=>"create", :controller=>"foos"}

但没有 GET 选项,通常为:

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

下面我将向您展示我的实际代码,以帮助我恢复丢失的索引路由。

routes.rb:

resource :announcements, :controller => "announcements" do
  resources :comments
  member do
    post 'vote'
  end
end

公告部分的路线:

announcements      POST   /announcements(.:format)      {:action=>"create", :controller=>"announcements"}
new_announcements  GET    /announcements/new(.:format)  {:action=>"new", :controller=>"announcements"}
edit_announcements GET    /announcements/edit(.:format) {:action=>"edit", :controller=>"announcements"}
                   GET    /announcements(.:format)      {:action=>"show", :controller=>"announcements"}
                   PUT    /announcements(.:format)      {:action=>"update", :controller=>"announcements"}
                   DELETE /announcements(.:format)      {:action=>"destroy", :controller=>"announcements"}

如您所见,没有获取/索引。在我的控制器中,我定义了简单的索引方法...

def index
  @announcements = Announcement.all

  respond_to do |format|
    format.html
    format.xml  { render :xml => @announcements }  
  end
end

我真的不明白为什么我没有这个索引路径。它也发生在其他几个控制器上。任何帮助将不胜感激。

编辑:在控制台中,app.announcements_path 除了其他缺少索引路径的方法外,还返回方法缺失错误。

【问题讨论】:

    标签: ruby-on-rails routes rake


    【解决方案1】:

    这是因为您使用的是 resources (resource) 的单数化版本。没有为这些生成 index 操作路线。您应该将其更改为复数版本,并从该行中删除 :controller

    【讨论】:

    • 非常感谢,我知道我这样做很愚蠢。赞一个~
    • 哇,我也错过了...谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2021-07-12
    • 1970-01-01
    相关资源
    最近更新 更多