2013-04-16 45 views
0

我試圖獲取10條最新文章的標題,這是博客應用程序在rails上使用ruby的一部分。我做到了。但是當談到路由時,我陷入了困境。顯示路由錯誤點擊軌道上的紅寶石鏈接時沒有路由匹配錯誤

當我不喜歡

(此代碼是文章/指數的一部分)

<% @article_titles.each do |article_title|%> 
       <% if !article_title.nil? %> 
       <div style="margin-top:15px; margin-left:8px"> <%= link_to article_title.title, 
       article_path(article_title) %></div> 
      <% end %>  
      <% end %> 

它給了我路由錯誤

沒有路由匹配{:動作=> 「秀」, :controller =>「articles」,:id =>#}錯誤。

我嘗試另一種方式,如下圖所示: -

<% @article_titles.each do |article_title|%> 
       <% if !article_title.nil? %> 
       <div style="margin-top:15px; margin-left:8px"> <%= link_to article_title.title, 
       "/articles?id=#{article_title.id}" %></div> 
      <% end %>  
      <% end %> 

的routes.rb

match "articles/:id" => "articles#show" 

我沒有給出錯誤,只顯示在我的browserwith沒有行動發生的地址欄中("http://localhost:3000/articles?id=")

耙路線:

 new_user_session GET /users/sign_in(.:format)       devise/sessions#new 
      user_session POST /users/sign_in(.:format)       devise/sessions#create 
    destroy_user_session DELETE /users/sign_out(.:format)       devise/sessions#destroy 
      user_password POST /users/password(.:format)       devise/passwords#create 
     new_user_password GET /users/password/new(.:format)      devise/passwords#new 
     edit_user_password GET /users/password/edit(.:format)     devise/passwords#edit 
         PUT /users/password(.:format)       devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)       devise/registrations#cancel 
     user_registration POST /users(.:format)         devise/registrations#create 
    new_user_registration GET /users/sign_up(.:format)       devise/registrations#new 
    edit_user_registration GET /users/edit(.:format)        devise/registrations#edit 
         PUT /users(.:format)         devise/registrations#update 
         DELETE /users(.:format)         devise/registrations#destroy 
        root  /            articles#index 
     dashboard_index GET /dashboard(.:format)        dashboard#index 
         POST /dashboard(.:format)        dashboard#create 
      new_dashboard GET /dashboard/new(.:format)       dashboard#new 
      edit_dashboard GET /dashboard/:id/edit(.:format)      dashboard#edit 
       dashboard GET /dashboard/:id(.:format)       dashboard#show 
         PUT /dashboard/:id(.:format)       dashboard#update 
         DELETE /dashboard/:id(.:format)       dashboard#destroy 
        tags GET /tags(.:format)         tags#index 
         POST /tags(.:format)         tags#create 
       new_tag GET /tags/new(.:format)        tags#new 
       edit_tag GET /tags/:id/edit(.:format)       tags#edit 
        tag GET /tags/:id(.:format)        tags#show 
         PUT /tags/:id(.:format)        tags#update 
         DELETE /tags/:id(.:format)        tags#destroy 
     article_comments GET /articles/:article_id/comments(.:format)   comments#index 
         POST /articles/:article_id/comments(.:format)   comments#create 
    new_article_comment GET /articles/:article_id/comments/new(.:format)  comments#new 
    edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit 
     article_comment GET /articles/:article_id/comments/:id(.:format)  comments#show 
         PUT /articles/:article_id/comments/:id(.:format)  comments#update 
         DELETE /articles/:article_id/comments/:id(.:format)  comments#destroy 
           /articles/:article_id/articles/:id(.:format)  articles#show 
       articles GET /articles(.:format)        articles#index 
         POST /articles(.:format)        articles#create 
      new_article GET /articles/new(.:format)       articles#new 
      edit_article GET /articles/:id/edit(.:format)      articles#edit 
       article GET /articles/:id(.:format)       articles#show 
         PUT /articles/:id(.:format)       articles#update 
         DELETE /articles/:id(.:format)       articles#destroy 

articles_controller.rb

def index 
      @articles = Article.all(:order => "created_at DESC") 
     @article_titles = Article.select(:title).first(10) 
     end 
    def show 
     @article = Article.find(params[:id]) 

    end 

的routes.rb

Mau::Application.routes.draw do 
    devise_for :users 
    root :to => 'articles#index' 
    resources :dashboard 
    resources :tags 
    resources :articles do 
    resources :comments 
    match "articles/:id" => "articles#show" 
end 

調試日誌。

ActionController::RoutingError (No route matches [GET] "/assets/defaults.js"): 
    actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
    actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
    railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app' 
    railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call' 
    activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged' 
    railties (3.2.11) lib/rails/rack/logger.rb:16:in `call' 
    actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
    rack (1.4.5) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.4.5) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.4.5) lib/rack/lock.rb:15:in `call' 
    actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call' 
    railties (3.2.11) lib/rails/engine.rb:479:in `call' 
    railties (3.2.11) lib/rails/application.rb:223:in `call' 
    rack (1.4.5) lib/rack/content_length.rb:14:in `call' 
    railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call' 
    rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service' 
    c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 

我是否需要在文章的show action中添加一些內容?請建議我。

回答

1

你的問題是,既然你選擇了@article_titles記錄不包含id屬性只有標題Article.select(:title)。一個更好的辦法來完成你想將在你的控制器是什麼:

@articles = Article.order("created_at DESC") 
@first_articles = @articles.limit(10) 

使用@first_articles你在哪裏使用@article_titles。

我也懷疑你是否需要@articles(你是否只顯示前10條記錄?)用代碼我建議它是一個ActiveRecord :: Relation,它不會被加載(命中數據庫),除非你實際上使用視圖中的變量,但是這是一個不同的主題SO質疑

+0

你說得對。然而,其他解決方案肯定會在選擇 – fotanus

+0

中添加「id」,但整個事情很尷尬;這樣做幾乎沒有任何好處。只要保持簡單,並讓數組中的每個元素都是一個Article實例。 – boulder

+0

嗨,巨石,非常感謝。它對我有用@first_articles = @ articles.first(10)的小改動。當它嘗試使用極限(10)時,它顯示錯誤未定義的方法'極限'爲#。現在它與第一個(10)一起工作。這是正確的做法。請提出任何建議。再次非常感謝。 –

0

刪除match "articles/:id" => "articles#show",確保resources :articles存在,並article_path(@article)應該工作。

變化

Article.select(:title).first(10) 

通過

Article.select([:id, :title]).first(10) 

你總是需要選擇:ID與航線使用。

+0

喜fotanus,我已經有資源:在routes.rb中文件的文章和你的建議嘗試,但它給了我路由錯誤 沒有路線匹配{:action =>「show」,:controller =>「articles」,:id => nil}錯誤。 –

+0

你可以發佈'logger.debug @ articles_title'嗎? – fotanus

+0

我已添加日誌以提問。請看看那裏。 –

1

這可能會或不會解決你的問題,但是你錯過了年底end在你的路由阻塞:

Mau::Application.routes.draw do 
    devise_for :users 
    root :to => 'articles#index' 
    resources :dashboard 
    resources :tags 
    resources :articles do 
    resources :comments 
    match "articles/:id" => "articles#show" 
end 

應該

Mau::Application.routes.draw do 
    devise_for :users 
    root :to => 'articles#index' 
    resources :dashboard 
    resources :tags 
    resources :articles do 
    resources :comments 
    end 
end 

請注意,你不應該需要match路線你有,因爲它是resources :articles的一部分。

編輯

你也應該嘗試通過僅在ID是這樣的:

<% @article_titles.each do |article_title|%> 
    <% if !article_title.nil? %> 
     <div style="margin-top:15px; margin-left:8px"> 
      <%= link_to article_title.title, article_path(article_title.id) %> 
     </div> 
    <% end %>  
<% end %> 
+0

喜鮎魚,我開始嘗試這樣才,但不能得到輸出。我想,我缺少的文章控制器的顯示作用的東西,因爲當我喜歡烏爾建議它給我路由錯誤 沒有路由匹配{:動作=> 「秀」,:控制器=> 「文章」,:ID =>#<文章大標題: 「LinkedIn」>}錯誤,這表明阿布t「展示」行動。 –

+0

試試我更新的答案。請記住在對路由進行任何更改後重新啓動服務器。 – Catfish