首页 > 解决方案 > 除了默认的 Rails 页面之外,无法呈现任何内容

问题描述

我正在按照教程 ( https://blog.teamtreehouse.com/static-pages-ruby-rails ) 使用 Rails 路由呈现页面。问题是我得到一个错误而不是应该呈现的页面。我不知道我做错了什么,因为实际上只有几个步骤。

我的代码与教程的唯一区别是微不足道的。我正在使用Videos而不是Pages.

config/routes.rb

Rails.application.routes.draw do
    # This means that all paths following the pattern:
    # /videos/about, /videos/home, /videos/features
    # will route here!
    get "/videos/:video" => "videos#show"
end

app/controllers/videos_controller.rb"

class VideosController < ApplicationController
    def show
        render template: "videos/#{params[:page]}"
    end
end

最后,我有一个充满乱码的静态页面存储在app/views/videos/videos.html.erb.

但是,当我运行服务器并转到时,0.0.0.0:3000/videos/我收到以下错误:

No route matches [GET] "/videos"
Rails.root: /home/mightu/Desktop/portal_rails

当我尝试0.0.0.0:3000/videos/pizza时,我得到:

Missing template /videos with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/mightu/Desktop/portal_rails/app/views" 

它告诉我问题是这一行:

render template: "videos/#{params[:page]}"

请帮忙谢谢

标签: ruby-on-railsruby

解决方案


你需要定义路由,如果你需要所有的 restful 路由 /videos,你可以添加一个,或者只添加.resources :videosget 'videos', to: 'videos#index'

您只定义了show动作的路线,但您正在尝试获取index视频页面,您可以在此处查看路由的详细信息


推荐阅读