我試圖獲取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中添加一些內容?請建議我。
你說得對。然而,其他解決方案肯定會在選擇 – fotanus
中添加「id」,但整個事情很尷尬;這樣做幾乎沒有任何好處。只要保持簡單,並讓數組中的每個元素都是一個Article實例。 – boulder
嗨,巨石,非常感謝。它對我有用@first_articles = @ articles.first(10)的小改動。當它嘗試使用極限(10)時,它顯示錯誤未定義的方法'極限'爲#。現在它與第一個(10)一起工作。這是正確的做法。請提出任何建議。再次非常感謝。 –