Search code examples
ruby-on-railsforms

No route matches [GET] "/show/:id". Show Action route not fetching in Rails


I am Newbie to Rails , so ignore my silly bugs and doubts

I am trying to add a view link in my index of Gods so as to show the details associated with that object( God ) here is shown

My gods_controller

class GodsController< ApplicationController
    def index
        @g=God.all
    end
    def new
        @g=God.new()
    end

    def show
        @g=God.find(params[:id])
        puts @g.inspect
    end

    def create
        @g=God.new(
            name: params[:god][:name],
            ph_no: params[:god][:ph_no],
            bhakts: params[:god][:bhakts]
        )
        if @g.save
            redirect_to '/gods/index'
        else
            render :new, status: :unprocessable_entity
        end
    end
end

show view

<h1><strong> ** GOD DETAILS !! ** </strong></h1>
<p><strong> Name : </strong><%= @g.name %></p>
<p><strong> Contact : </strong><%= @g.ph_no %></p>
<p><strong> Number Of Bhakts : </strong><%= @g.bhakts %></p>

my index action which has table of Gods and view link

<h1> <b> MY GODS !! </b></h1>

<table>
<thead>
<tr>
<th> Name</th>
<th> ph_no</th>
<th> Bhakts</th>
</tr>
</thead>
<tbody>
 <% @g.each do |x| %>
 <tr>
 <td><%= x.name%></td>
 <td><%= x.ph_no%></td>
<td><%= x.bhakts%></td>
<td><%= link_to 'view', 'gods/show/:id' %> --------> "what should i write here to show/:id
</tr>
<% end%>
</tbody>
</table>

Schema.rb file

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_04_18_112327) do
  create_table "gods", force: :cascade do |t|
    t.string "name"
    t.string "ph_no"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "bhakts"
  end

end

Also i have tried many different ways such as god and god_path(@g) there but it then shows nil id

I was expecting to view God details with id x by clicking the view link in index page


Solution

  • If we can assume that you have defined the route with resources :gods you can just pass a instance of God:

    <%= link_to 'view', x %>
    

    Or if you want to be more explicit:

    <%= link_to 'view', god_path(x) %>
    

    The idiomatically correct path to display a resource in rails is /gods/:id. "show" should never be included in the path.

    See: