【问题标题】:showing Routing Error No route matches error when clicking on link in ruby on rails app单击 ruby​​ on rails 应用程序中的链接时显示路由错误没有路由匹配错误
【发布时间】:2013-04-16 15:44:26
【问题描述】:

我尝试使用 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 %>

routes.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

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'

我需要在文章的显示操作中添加一些内容吗?请给我建议。

【问题讨论】:

    标签: ruby-on-rails ruby routing


    【解决方案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 问题的主题

    【讨论】:

    • 你是对的。但是其他解决方案是在选择中添加“id”
    • 当然,但整个事情很尴尬;这样做几乎没有任何好处。保持简单,让数组中的每个元素都是 Article 实例。
    • 嗨巨石,非常感谢。它对我有用,只需少量更改@first_articles = @articles.first(10)。当它尝试使用limit(10) 时,它显示#<0x4d5c608>
    【解决方案2】:

    这可能会或不会解决您的问题,但您的路线块中缺少 end 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 %>
    

    【讨论】:

    • 嗨鲶鱼,我开始尝试这样,但无法获得输出。我想,我在文章控制器的显示操作中遗漏了一些东西,因为当我喜欢你的建议时,它给了我路由错误没有路由匹配 {:action=>"show", :controller=>"articles", :id=>#} 错误,表示关于“显示”操作。
    • 试试我更新的答案。请记住在对路由进行任何更改后重新启动服务器。
    【解决方案3】:

    删除match "articles/:id" =&gt; "articles#show",确保resources :articles 存在,article_path(@article) 应该可以工作。

    改变

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

    通过

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

    您总是需要选择 :id 才能与路线一起使用。

    【讨论】:

    • 嗨 fotanus,我已经在 routes.rb 文件中有资源 :articles 并尝试了你的建议,但它给了我 Routing Error No route matches {:action=>"show", :controller=>"文章”,:id=>nil} 错误。
    • 你能发一个logger.debug @articles_title吗?
    • 嗨,谢谢。我要问的是把上面的行放在你的控制器中。然后在您的服务器输出中,您应该会看到一行显示@articles_title 的内容。您也可以使用调试器并检查它以获得相同的结果
    • 在你的行动中,你可以写logger.debug "yey"之类的东西。这将在服务器的输出中打印 yey,通常是您原始的终端 rails server。所以你也可以打印变量。我的意思是输入logger.debug @articles_title.inspect 并检查服务器输出上出现的内容。明白了吗?
    • this 可能有帮助
    猜你喜欢
    • 2016-08-28
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多