2013-12-11 44 views
0

我創建了一個表單來創建文章,但我只能在索引頁上看到已發佈的文章,而且我無法查看單個文章。無法顯示Ruby on Rails上的文章?

每次點擊索引上單個文章的「顯示」時,它都會打開一個空白頁面,其URL爲localhost:3000/articles.1而不是localhost:3000/articles/1

我不知道代碼有什麼問題,它與我用於創建和編輯文章的代碼類似於管理員,它在那裏工作,但我不斷收到此錯誤。

下面的代碼:

的意見/用品/ show.html.erb:

<h1><%= @article.name %></h1> 
<%= @article.content %> 
<%= image_tag @article.photo.url(:small) %> 

<p>Tags: <%= raw article.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %></p> 
<%= link_to 'Back', noticias_path %> 

的意見/用品/ index.html.erb:

​​

articles_controller.rb:

# GET /articles/1 
# GET /articles/1.json 
def show 
    @article = Article.find(params[:id]) 
end 

潰敗es.rb:

Blog::Application.routes.draw do 
    root to: 'welcome#index' 
    get 'tags/:tag', to: 'noticias#index', as: :tag 
    get "noticias/index" 
    get "dashboard/index" 
    get "/sitemap" => "sitemap#index", :as => :sitemap, :defaults => {:format => :xml} 
    match '/noticias', to: 'noticias#index', via: 'get' 
    get 'login', to: 'sessions#new', as: 'login' 
    get 'logout', to: 'sessions#destroy', as: 'logout' 
    resources :users 
    resources :sessions 
    namespace :admin do 
    get '', to: 'dashboard#index', as: '/' 
    resources :noticias 
    end 
end 
+1

顯示你的'routes.rb' –

+0

@LuisLago我發佈你的routes.rb在你的問題。您可以在下次編輯問題而不是將其粘貼到評論中;否則很難閱讀。 –

+1

謝謝,我會記住,下次 – LuisLago

回答

1

它更具有noticias_path一個問題,考慮rake routes驗證您的路線,但我想改變noticias_pathnoticia_path,可能可以固定它。

+0

不,它不能解決它。在路線中,我有這個'match'/ noticias',以:'articles#index',via:'get''。我用英文創作了這些表格,因爲我正在用英文進行一些教程,但後來我創建了一條通往Notícias的路線,因爲該網站將以葡萄牙語顯示。我是否應該在所有代碼中將「articles」更改爲「noticias」以避免此問題? – LuisLago

+0

@LuisLago爲了澄清,你的意思是說你*只*具有該索引路線,而沒有別的(意味着你沒有指向'show'動作的行)?這確實聽起來像一個路由問題,但你必須發佈你的整個routes.rb才能看到。如果您將某些路線重新命名爲葡萄牙語,聽起來像您可能錯過了某些東西。 –

+0

@LuisLago,你可以發佈routes.rb的內容來澄清? –

0

根據您的路線文件,看起來問題確實是不正確的路由。目前,您正在手動爲RESTful資源創建路由,而您應該讓rails爲您正確處理。

卸下手動getmatch行:

get "noticias/index" 

match '/noticias', to: 'noticias#index', via: 'get' 

並替換它們與此:

resources :noticias 

如果noticias應指向ArticlesController(或任何其它控制器),然後簡單地做這個:

resources :noticias, to: 'articles' 

此外,馬修斯卡塞雷斯是正確的,該URL幫手實際上應爲noticia_pathshow行動。 noticias_path指向index操作。 Rails嘗試通過使用路由,幫助器,函數等來「有幫助」,如果用英語閱讀,聽起來是正確的,但對於另一種語言的單詞可能不一定有意義。我不知道「noticia」在葡萄牙語中是否有意義。

另一個快速提示,我上面指出的兩個手動路由線是冗餘的;他們實際上都會匹配GET請求並將它們路由到noticias#index。但是,請記住,路由按照路由文件中顯示的順序進行匹配,因此match行永遠不會被調用,因爲路由在該行上將與該行匹配。