All courses is are showing on index.html.erb and I am trying to view an individual course by clicking view more. Which should link to the show path as I understand, it display that individual course. Which is the result I need.
However, rails throughs this error on the URL http://ruby-on-rails-102039.nitrousapp.com:3000/courses/rails (within this URL rails is title of the course, the title is created with each new course.
ActiveRecord::RecordNotFound in CoursesController#show
Couldn't find Course with 'id'=rails
Extracted source (around line #7):
def show @course = Course.find(params[:id]) end
Course controller
class CoursesController < ApplicationController
def index
@courses = Course.all
end
def show
@course = Course.find(params[:id])
end
Routes
devise_for :users
root 'signups#new'
resources :signups, only: [:new, :create]
resources :courses
Prefix Verb URI Pattern Controller#Action
root GET / signups#new
root GET / signups#new
signups POST /signups(.:format) signups#create
new_signup GET /signups/new(.:format) signups#new
courses GET /courses(.:format) courses#index
POST /courses(.:format) courses#create
new_course GET /courses/new(.:format) courses#new
edit_course GET /courses/:id/edit(.:format) courses#edit
course GET /courses/:id(.:format) courses#show
PATCH /courses/:id(.:format) courses#update
PUT /courses/:id(.:format) courses#update
DELETE /courses/:id(.:format) courses#destroy
index.html.erb
<div id="course-index">
<%@courses.each_slice(4) do|course| %>
<div class ="row">
<% course.each do |course|%>
<div class="col-md-3 col-sm-3">
<h3><%= course.title %></h3><br />
<h3><%= course. description %></h3><br />
<%= link_to 'View More', course_path(course), class:'btn btn-primary' %>
</div>
<%end%>
</div>
<%end%>
</div>
Model
class CreateCourses < ActiveRecord::Migration
def change
create_table :courses do |t|
t.string :title
t.string :desciption
t.integer :course_id
t.timestamps null: false
end
end
end
show.html.erb
<h1>Courses#show</h1>
<h1><%= @course.title %></h1>
<p> <%= @course.description %></p>
Solution: All three of these worked in the show method. I just don't understand why @course = Course.find(params[:id]). Is it because I have these columns defined in the model.
@course = Course.find_by(params[:title])
@course = Course.find_by(params[:course_id])
@course = Course.find_by(params[:description])