ruby-on-railsrubyroutesviewcontroller

Routes are not getting available using resources in Rails


I am trying to learning Ruby On Rails MVC framework by implementing a test application where I am trying to create form submission and listing records. I am using resources way to define my routes to controller in my router.rb file. But when I am trying to load page using controller No route matches error I am getting,

 `No route matches [GET] "/new"`

My router file routes.rb like the folllowing ,

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

   #get 'home/:id', to: 'articles#home'
   #get 'details', to: 'articles#details'
   #get 'new', to:'articles#new'

   resources :articles

 end

controller articles_controller.rb

class ArticlesController < ApplicationController
  def home

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

  end
  def details

    @articledetails = Article.all

  end

  def new
   
  end

  def create
    @article = Article.new(params.require(:article).permit(:title, :description))
    @article.save
    redirect_to @article
  end 

 end

My view file new.html.erb,

<h1>Create a new article</h1>

<%= form_with scope: :article, url: articles_path, local: true do |f| %>
  <p> 
    <%= f.label :title %><br/> 
    <%= f.text_field :title %>
  </p>
 <p>
    <%= f.label :description %><br/> 
    <%= f.text_area :description %> 
 </p>
 <p>
     <%= f.submit %> 
     </p>
  <% end %>

Trouble Shooted Way,

I applied the following command in project root directory command prompt. But it not listing articles route.

rails routes --expanded

Here what to do to enable the routes according to router.rb definition? Anyone can anyone suggest or guide me to resolve this error please? or give any documentation to refer?


Solution

  • In routes.rb you have

    #get 'new', to:'articles#new'
    

    but should have

    get 'new', to:'articles#new'
    

    Then you will have /new url

    Or if you want to use resources :articles

    then your url in browser should be /articles/new (not /new)