我尝试使用 ruby on rails 获取 10 篇最新文章的标题,这是博客应用程序的一部分。我这样做了。但是在路由方面我被卡住了。
当我喜欢
(此代码是文章/索引的一部分)
<% @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 %>
它给了我路由错误
没有路线匹配 {:action=>"show", :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 %>
路线.rb
match "articles/:id" => "articles#show"
我没有给出任何错误,只显示("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
文章控制器.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
路线.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'
我需要在文章的展示动作中添加一些东西吗?请给我建议。