2017-04-07 79 views
1

我正在關注article以測試站點地圖生成。該文章沒有提及任何關於編寫show的行動。但是,自從我開始得到這個錯誤,我甚至寫了一個簡單的show操作。以下是我的帖子控制器:沒有路線匹配{:action =>「show」....:controller =「posts」}

def index 
    @posts= Post.all 
end 

def show 
@posts = Post.all 
end 

的routes.rb

Rails.application.routes.draw do 
    resources :categories do 
     resources :posts 
    end 
    root to: 'pages#index' 
end 

sitemap.rb

Category.find_each do |category| 
    add category_posts_path(category), :changefreq => 'weekly', :lastmod => category.updated_at 

    category.posts.each do |post| 
     add category_post_path(category), :changefreq => 'yearly', :lastmod => post.updated_at 
    end 
    end 

當我運行rake sitemap:refresh,它給以下:

In '/home/mypc/Projects/sitemaptest/public/': 
rake aborted! 
ActionController::UrlGenerationError: No route matches {:action=>"show", :category_id=>#<Category id: 1, title: "Surprised by Joy", created_at: "2017-04-07 09:13:52", updated_at: "2017-04-07 09:13:52">, :controller=>"posts"} missing required keys: [:id] 

回答

2

我想你錯過post PARAM在:

category.posts.each do |post| 
    add category_post_path(category), :changefreq => 'yearly', :lastmod => post.updated_at 
end 

它應該是:

category.posts.each do |post| 
    add category_post_path(category, post), :changefreq => 'yearly', :lastmod => post.updated_at 
end 

希望它能幫助。

1

你應該使用這樣的:

Category.find_each do |category| 
    add category_posts_path(category), :changefreq => 'weekly', :lastmod => category.updated_at 

    category.posts.each do |post| 
     add category_post_path(category, post), :changefreq => 'yearly', :lastmod => post.updated_at 
    end 
    end 

你失蹤後的對象,這是nested路線,這就是爲什麼越來越missing required keys: [:id]問題。

相關問題