Ruby on rails 如何将my courses/show.html.erb链接到当前课程?
我正在创建一个应用程序,允许用户通过视频访问不同的课程(每个课程都有自己的4或5个视频,每个视频都有自己的页面) 我创建了一个Ruby on rails 如何将my courses/show.html.erb链接到当前课程?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我正在创建一个应用程序,允许用户通过视频访问不同的课程(每个课程都有自己的4或5个视频,每个视频都有自己的页面) 我创建了一个courses\u控制器,它允许我索引和显示存储在数据库中的课程 课程(u controller.rb): class CoursesController < ApplicationController before_action :set_course, only: [:show] skip_before_filter :verify_authenticity_
courses\u控制器
,它允许我索引和显示存储在数据库中的课程
课程(u 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
class CoursesController“您的订阅页面”
终止
终止
每个课程都存储在seeds.rb文件中
courses/index.html.erb
列出课程,courses/show.html.erb
显示特定课程的演示。这两部分都可以
如何从show.html.erb页面创建指向当前课程的链接
我的意思是,如果我有“course1”和“course2”,链接将重定向“(show.html.erb)course1演示文稿”到“(?.html.erb)course1第一个视频”和“(show.html.erb)course2演示文稿”到“(?.html.erb)course2第一个视频”,等等
有什么想法吗?在你的索引上,你做得对
def index
@courses = Course.all
end
现在在索引视图中,您应该有链接来显示与每个课程相关的页面
<% @courses.each do |course| %>
<tr>
....
....
<td><%= link_to( 'show', course_path(course) ) %></td>
</tr>
<% end %>
现在您将在first show request和每次分页单击中获得第一个视频。你会看到下一个
<%= @course.name %>
<%= @videos.first.url %> ##because paginate return array always
## to paginate videos
<%= will_paginate @videos %>
##因为分页返回数组总是
##为视频分页
你在这里所说的当前课程是什么意思?我的意思是,如果我有“course1”和“course2”,链接会将“course1演示”重定向到“course1 first video”,将“course2演示”重定向到“course2 first video”
<%= @course.name %>
<%= @videos.first.url %> ##because paginate return array always
## to paginate videos
<%= will_paginate @videos %>