ruby-on-railsrubyroutescontrolleraction

No route matches Error in Controller and Router using Ruby on Rails MVC


I am new to ruby on rails. And I am trying to test one simple record pull example using Active Record. I am getting the following error while giving action from router to controller to get some article details using ruby on rails framework,

No route matches [GET] "/home/1"

My router file content like the following,

Rails.application.routes.draw do

get "up" => "rails/health#show", as: :rails_health_check
get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
get "manifest" => "rails/pwa#manifest", as: :pwa_manifest

resource :pages do
 get 'home' => 'pages#home'
end
end

controller pages_controller.rb like the following,

class PagesController < ApplicationController
 def home

    @article = Article.find(params[:id])

 end
end

Home.html.erb view file look like the following ,

<div>

 <p> <% @article.title  %> </p>
 <p> <% @article.description  %></p>

</div>

And this file is under views/pages folder.

Can anyone suggest or guide me to resolve this error please? or give any documentation to refer?


Solution

  • Short answer

    Instead of

    resource :pages do
      get 'home' => 'pages#home'
    end
    

    use

    get 'home/:id', to: 'pages#home'