2015-08-26 29 views
0

我創建的應用程序允許用戶訪問視頻上的不同課程(每個課程都有自己的4或5個視頻,每個視頻都有自己的頁面)。如何將我的課程/ show.html.erb鏈接到current_course?

我創建了一個courses_controller,讓我索引並顯示存儲在數據庫中的課程。

Courses_controller.rb:

class CoursesController < ApplicationController 
before_action :set_course, only: [:show] 
skip_before_filter :verify_authenticity_token 
before_filter :authorize , except: [:index, :show] 

def index 
    @courses = Course.all 
end 

def show 
end 

private 
# Use callbacks to share common setup or constraints between actions. 
def set_course 
    @course = Course.find(params[:id]) 
end 

def authorize 
    unless User.find_by_id(session[:user_id]) and User.find_by_id(session[:user_id]).active == true 
     redirect_to subscriptions_path, :notice => "Vous devez souscrire à un abonnement pour avoir accès a cette page" 
    end 
end 

每個過程被存儲在一個文件seeds.rb。

courses/index.html.erb列出了課程,courses/show.html.erb顯示特定課程的呈現。這兩部分都可以。

如何創建從show.html.erb頁面給當前線路的鏈接?我的意思是,如果我有「course1」和「course2」,鏈接會將「(show.html.erb)course1 presentation」重定向到「(?.html.erb)course1 first video」和「(show .html.erb)course2呈現」到 「(?.html.erb)course2第一視頻」 等

任何想法?

+0

這是什麼意思'current_course'在這裏? – Pavan

+0

我的意思是,如果我有「course1」和「course2」,該鏈接將重定向「course1呈現」到「course1第一視頻」和「course2呈現」到「course2第一部影片」 –

回答

0

在你的索引你做對了。

def index 
    @courses = Course.all 
end 

現在在索引視圖中,您應該有鏈接顯示每個課程相關的頁面。

<% @courses.each do |course| %> 
    <tr> 
    .... 
    .... 
    <td><%= link_to('show', course_path(course)) %></td> 
    </tr> 
<% end %> 

要顯示來自第一視頻相關的課程顯示頁開始的所有視頻,您應該使用雷或將分頁。所以你的演出行動應該看起來像。

def show 
    @course = Course.find(params[:id]) #if you want to show any detail of course 
    @videos = @course.videos.paginate(:page => params[:page], :per_page => 1) 
end 

現在,您將在首次展示請求和每個分頁點擊時獲得第一個視頻。你會看到下一個。

<%= @course.name %> 
<%= @videos.first.url %> ##because paginate return array always 

## to paginate videos 

<%= will_paginate @videos %> 
+0

這是我所期待的,謝謝:) –

相關問題