0
votes

I tried to fetch 10 most recent articles's title which is part of blog app using ruby on rails. I did this. But i got stuck when it comes to routing.

When I do like

(this code is part of article/index)

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

It gives me Routing Error

No route matches {:action=>"show", :controller=>"articles", :id=>#} error.

I tried another way, like below:-

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

I gives no error and only showing ("http://localhost:3000/articles?id=") in address bar of my browserwith no action taken place.

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

debugger logs.

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'

Do i need to add something in show action of article? Please suggest me.

3

3 Answers

1
votes

Your problem is that the @article_titles records don't contain an id attribute since you're selecting only the title Article.select(:title). A much better way to accomplish what you're trying would be to have in your controller:

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

Use @first_articles where you were using @article_titles.

I also question whether you need @articles at all (are you only displaying the first 10 records?) With the code I am suggesting it's an ActiveRecord::Relation and it won't be loaded (hit the database) unless you're actually using the variable in your view, but that's a topic for a different SO question

1
votes

This may or not solve your problem, but you're missing end end in your routes block:

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

should be

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

Note that you shouldn't need the match route that you have because it's part of the resources :articles.

EDIT

You should also try passing in only the id like this:

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

remove match "articles/:id" => "articles#show", make sure resources :articles is present, and article_path(@article) should work.

Change

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

by

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

You always needs to select :id to use with routes.