user5278050
user5278050

Reputation:

No route matches {:action=>"show", :controller=>"schools"} missing required keys: [:id]

I don't know why I couldn't create an info link for every school in my project. Here is the full error:

No route matches {:action=>"show", :controller=>"schools"} missing required keys: [:id] <%= link_to "Info", school_path, class: "btn btn-info" %>

Here is my index.html.erb:

    <% @schools.each do |school| %>
      <%= school.name %>
      <%= link_to "Info", school_path, class: "btn btn-info" %>
      <br>
    <% end %>

schools_controller.rb:

class SchoolsController < ApplicationController
  def show
    @school = School.find(params[:id])
  end

  def new
    @school = School.new
  end

  def edit
    @school = School.find(params[:id])
  end

  def index
    @schools = School.all
  end

  def create
    @school = School.new(school_params)

    @school.save
    redirect_to @school
  end

  private

    def school_params
      params.require(:school).permit(:name)
    end
end  

Upvotes: 3

Views: 2925

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Because you're learning, I'll give you some context.

--

No route matches

The error means your Rails routes could not find the referenced route you had.

It gives the following specification:

missing required keys: [:id]

This means that the route exists, but you've not filled out the id parameter.

--

To help you understand how this works, consider the following:

   GET          /photos             photos#index    display a list of all photos
   GET          /photos/new         photos#new      return an HTML form for creating a new photo
   POST         /photos             photos#create   create a new photo
>> GET          /photos/:id         photos#show     display a specific photo
   GET          /photos/:id/edit    photos#edit     return an HTML form for editing a photo
   PATCH/PUT    /photos/:id         photos#update   update a specific photo
   DELETE       /photos/:id         photos#destroy  delete a specific photo

These are the routes created from using the resources directive in Rails. I won't explain about it right now - just saying that when you call school_path, you're referring to the GET /schools/:id path in your routes.

As such, what you have to remember is that when you invoke this route, you have to send an id to it -- url.com/schools/1. This allows the controller to look up the appropriate record (School.find params[:id]).

When you call...

<%= link_to "Info", school_path, class: "btn btn-info" %>

... you're not passing any param to school_path.

To fix it, you have to pass the id, which Rails will do automatically if you pass the appropriate object:

<%= link_to "Info", school_path(school), class: ".." %>

or

<%= link_to "Info", school, class: "..." %>

Upvotes: 4

beydogan
beydogan

Reputation: 1110

You need pass school object or school id to school_path method. So it can create a path by that id. You can do it as following;

<% @schools.each do |school| %>
  <%= school.name %>
  <%= link_to "Info", school_path(school), class: "btn btn-info" %>
  <br>
<% end %>

Upvotes: 2

Related Questions