1

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])
user3905353
  • 77
  • 2
  • 12
  • Can you show us how your courses table looks like, i.e. what columns did you define. I guess the migration file where you've added courses model should do fine. – Marko Gresak Jan 15 '16 at 22:18
  • You seem to have two nested each loops both defining course, did you mean to do that? I've no idea what happens, but it looks a bit odd – Tim Jan 15 '16 at 22:22
  • @Tim The OP is outputting the courses 4 to a row - thats why there are nested loops - the variable naming choices for the loop variables aren't great – Thomas Walpole Jan 16 '16 at 00:46
  • @Tom Yeah, I was a bit tired, looking at it last night, i was worried it might overwrite something, but looking now, it doesn't. – Tim Jan 16 '16 at 10:54

2 Answers2

1

@course = Course.find(params[:id]) finds the course by the id number, but you're passing in the title instead of the id number. If thats the way you want to keep it

@course = Course.find_by(title: params[:id])  

will probably do what you want

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Added your solution as the on that worked for me, with all model columns in table. Do you know why @course = Course.find_by(title: params[:id]) does not work? – user3905353 Jan 16 '16 at 01:38
  • Sorry - I don't understand the question? You're saying it did work for you, but then asking why it does not work? – Thomas Walpole Jan 16 '16 at 01:44
  • It works when I when I removed

    <%= course.title %>

    and

    <%= course.description %>

    from show.html.erb. If want it to work with show.html.erb with course.title, course.description, I have to use the solution I posted up top. I was saying params [:id] by itself will not work period.
    – user3905353 Jan 16 '16 at 01:56
  • It sounds like you are confused by rails routing. If you look at the route /courses/:id(.:format) it shows you that if you navigate to /courses/my_course - then params[:id] will be 'my_course' -- you then will need to find that in whatever column you want to look for it. Course.find(x) will find the Course record with a primary_key column == x (and if the primary key column is numeric it will convert x to a numeric) - Course.find_by(...) takes a hash and will find the record with all of those hash elements matching -- Course.find_by(title: 'abc', description:'my desc') - etc. – Thomas Walpole Jan 16 '16 at 02:01
  • Also the course_id field in your model is strange naming -- Normally in rails naming that would imply a belongs_to association to another course? The course model already has a default auto-incrmenting #id field created when you do create_table in your migration – Thomas Walpole Jan 16 '16 at 02:06
  • Thanks you very much for explaining that. Course_id does have an association with lessons that belong to that course. – user3905353 Jan 16 '16 at 02:13
  • umm -- if it's associated to lessons then why is it called course_id in the course table -- it should be in the lesson table/model so that you can have multiple lessons linked to a course – Thomas Walpole Jan 16 '16 at 02:18
0

you can try this:

#route.rb
resources :courses, params :title

this route:course GET /courses/:id(.:format) courses#show

become like this:course GET /courses/:title(.:format) courses#show

#controller.rb

#like said Tom Walpole you can do this:
@course = Course.find_by(title: params[:title])
#but I prefer this syntax:
@course = Course.find_by_title(params[:title])

If you have an error, try this in the viex: course_path(course.title)

jpheos
  • 448
  • 5
  • 12