您没有遵循生成控制器的约定。生成控制器不会创建数据库表。你必须通过调用rails generate model、rails generate resource 或rails generate scaffold 来做到这一点。
因此,您需要一个用于几个静态页面的控制器。试试这个
rails generate controller static_pages home help contact
请注意生成器是复数和蛇形大小写(static_pages)。这将生成静态控制器和home.html.erb、help.html.erb 和contact.html.erb 页面
现在您可以在控制器中使用这些操作访问页面
def home
end
def help
end
def contact
end
还需要确保路由设置好
# routes.rb
match '/home', to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
没有设置数据库,您可以访问这些页面。这就是你需要做的所有事情。只需遵循约定,例如复数控制器和单数模型,并且 rails 负责处理细节。希望这能让你开始
更新
响应 cmets 这里是生成控制器的标准输出。请注意,我的示例使用 haml 而不是 erb,但输出中没有与数据库相关的任何内容。
rails g controller static_pages home help contact
create app/controllers/static_pages_controller.rb
route get "static_pages/contact"
route get "static_pages/help"
route get "static_pages/home"
invoke haml
create app/views/static_pages
create app/views/static_pages/home.html.haml
create app/views/static_pages/help.html.haml
create app/views/static_pages/contact.html.haml
invoke rspec
create spec/controllers/static_pages_controller_spec.rb
create spec/views/static_pages
create spec/views/static_pages/home.html.haml_spec.rb
create spec/views/static_pages/help.html.haml_spec.rb
create spec/views/static_pages/contact.html.haml_spec.rb
invoke helper
create app/helpers/static_pages_helper.rb
invoke rspec
create spec/helpers/static_pages_helper_spec.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss