【问题标题】:How to link my courses/show.html.erb to the current_course?如何将我的课程/show.html.erb 链接到 current_course?
【发布时间】:2015-08-26 06:27:32
【问题描述】:

我正在创建一个应用程序,允许用户访问不同的视频课程(每个课程都有自己的 4 或 5 个视频,每个视频都有自己的页面)。

我创建了一个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

每门课程都存储在一个种子.rb 文件中。

courses/index.html.erb 列出课程,courses/show.html.erb 显示特定课程的介绍。这2个部分没问题。

如何从 show.html.erb 页面创建指向当前课程的链接?

我的意思是,如果我有“course1”和“course2”,链接会将“(show.html.erb)course1 演示文稿”重定向到“(?.html.erb)course1 第一个视频”和“(show. html.erb )course2 演示”到“( ?.html.erb )course2 第一个视频”等

有什么想法吗?

【问题讨论】:

  • 这里的current_course 是什么意思?
  • 我的意思是,如果我有“course1”和“course2”,链接会将“course1 演示”重定向到“course1 第一个视频”,“course2 演示”重定向到“course2 第一个视频”

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

在你的索引上你做得对。

def index
  @courses = Course.all
end

现在在索引视图中,您应该有显示与每门课程相关的页面的链接。

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

要在显示页面中从第一个视频开始显示与课程相关的所有视频,您应该使用 kaminari 或将分页。所以你的表演动作应该是这样的。

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 %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    相关资源
    最近更新 更多