2016-01-15 45 views
1

所有課程都顯示在index.html.erb上,我試圖通過單擊查看更多來查看單個課程。據我瞭解,它應該鏈接到顯示路徑,它顯示單個課程。這是我需要的結果。CoursesController中的ActiveRecord :: RecordNotFound#show

然而,軌道得來的URL http://ruby-on-rails-102039.nitrousapp.com:3000/courses/rails(此URL軌內這個錯誤是當然的標題,標題與每一個新的過程中產生。

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 

課程控制器

class CoursesController < ApplicationController 
def index 
@courses = Course.all 
end 

def show 
@course = Course.find(params[:id]) 
end 

路線

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> 

型號

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> 

解決方案:所有這三個在表演方法處理。我只是不明白爲什麼@課程= Course.find(params [:id])。是因爲我在模型中定義了這些列。

@course = Course.find_by(params[:title]) 
@course = Course.find_by(params[:course_id]) 
@course = Course.find_by(params[:description]) 
+0

您能向我們展示您的課程表的外觀如何,即您定義了哪些列。我猜你已經添加課程模型的遷移文件應該沒問題。 –

+0

你似乎有兩個嵌套的每個循環既定義當然,你的意思是這樣做?我不知道會發生什麼,但它看起來有點奇怪 – Tim

+0

@Tim OP正在將課程4輸出到一行 - 這就是爲什麼有嵌套循環 - 循環變量的變量命名選項不是很好 –

回答

1

@course = Course.find(params[:id])發現通過識別號碼的過程,但你傳遞的稱號,而不是ID號。如果這就是你想要的方式來保持它

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

可能會做你想要

+0

添加了您的解決方案,爲我工作,與表中的所有模型列。你知道爲什麼@course = Course.find_by(title:params [:id])不起作用嗎? – user3905353

+0

對不起 - 我不明白這個問題?你說它確實對你有用,但後來問爲什麼它不起作用? –

+0

當我刪除時,它起作用

<%= course.title%>

<%=當然。來自show.html.erb的說明%>

。如果想讓它與show.txt一起使用,請使用course.title,course.description,我必須使用我公佈的頂級解決方案。我說params [:id]本身不會工作。 – user3905353

0

你可以試試這個什麼:

#route.rb 
resources :courses, params :title 

這條路線:course GET /courses/:id(.:format) courses#show

變成了這個樣子: 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]) 

如果您有錯誤,請在viex中嘗試以下操作:course_path(course.title)

相關問題