ruby-on-rails - 除了默认的 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]}"
请帮忙谢谢
解决方案
你需要定义路由,如果你需要所有的 restful 路由
/videos
,你可以添加一个,或者只添加.resources :videos
get 'videos', to: 'videos#index'
您只定义了show
动作的路线,但您正在尝试获取index
视频页面,您可以在此处查看路由的详细信息
推荐阅读
- node.js - 在所有其他脚本之前运行 NPM 脚本
- javascript - 如何从 JS 中的实例化中获取类?
- build - ./toolchain.py:找不到命令(kivy)
- c# - 使用 WPF .Net.Core 从 windows 环境为 mac os 制作多平台应用程序
- javascript - 如何从输入字段编辑表数据的值
- php - 为什么这个 PHP 递归函数不起作用
- python - 在 Pandas 数据框中对行进行分组,应用自定义函数并将结果作为行存储在新数据框中
- python-3.x - 气流 - 在初始化期间修补函数
- rust - 使 struct 的默认值在 rust 中生效
- r - Caret train: 复制 rpart 时出错。“na.fail.default [on target class] 对象中的缺失值出错”