ruby on rails - ActiveRecord::RecordNotFound in CoursesController#show -
all courses showing on index.html.erb , trying view individual course clicking view more. should link show path understand, display individual course. result need.
however, rails throughs error on url http://ruby-on-rails-102039.nitrousapp.com:3000/courses/rails (within url rails title of course, title created each new course.
activerecord::recordnotfound in coursescontroller#show couldn't find course '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 / signups#new root / signups#new signups post /signups(.:format) signups#create new_signup /signups/new(.:format) signups#new courses /courses(.:format) courses#index post /courses(.:format) courses#create new_course /courses/new(.:format) courses#new edit_course /courses/:id/edit(.:format) courses#edit course /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 |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 |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: 3 of these worked in show method. don't understand why @course = course.find(params[:id]). because have these columns defined in model.
@course = course.find_by(params[:title]) @course = course.find_by(params[:course_id]) @course = course.find_by(params[:description])
@course = course.find(params[:id])
finds course id number, you're passing in title instead of id number. if thats way want keep it
@course = course.find_by(title: params[:id])
will want
Comments
Post a Comment