显示路由错误在rails app上的ruby中单击链接时没有路由匹配错误

时间:2013-04-16 15:44:26

标签: ruby-on-rails ruby routing

我尝试获取10篇最新文章的标题,这是使用ruby on rails的博客应用程序的一部分。我这样做了但是在路由方面我遇到了困难。

当我喜欢时

(此代码是文章/索引的一部分)

<% @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 =&gt;“show”,:controller =&gt;“articles”,:id =&gt;#}错误。

我尝试了另一种方式,如下所示: -

 <% @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"

我没有提供任何错误,只在浏览器的地址栏中显示("http://localhost:3000/articles?id=")而未执行任何操作。

rake routes:

        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动作中添加一些内容?请建议我。

3 个答案:

答案 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 %>

答案 1 :(得分:1)

您的问题是@article_titles记录不包含id属性,因为您只选择标题Article.select(:title)。完成你正在尝试的更好的方法是在你的控制器中:

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

在使用@article_titles的地方使用@first_articles。

我还怀疑你是否需要@articles(你只显示前10条记录吗?)使用代码我建议它是一个ActiveRecord :: Relation并且它不会被加载(命中数据库)除非你'实际上是在你的视图中使用变量,但这是一个不同的SO问题的主题

答案 2 :(得分:0)

删除match "articles/:id" => "articles#show",确保resources :articles存在,article_path(@article)应该有效。

更改

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

通过

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

您始终需要选择:ID以用于路线。