简单的 Ruby on Rails 应用程序
我想编写简单的应用程序,带有索引和关于页面,不使用数据库,所有文本都将在视图中。但我无法调用我的控制器方法来更改视图。 这是控制器(home_controller.rb)
class HomeController < ApplicationController
def index
end
def about
end
end
布局:
!!!
%html
%head
%title Title
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tags
%body
.wrapper
= yield
.footer
主页索引视图:
%h2 Index
%h1
This is example page
%p
= link_to "Home", root_path
= link_to "About", :controller => "home", :action => "index"
%p
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
关于视图:
%h2 About
%h1
This is about page
%p
= link_to "Home", root_path
= link_to "About", :controller => "home", :action => "about"
%p
Lorem ipsum
在routes.rb中我有root :to =>; 'home#index'
当我调用我的域时,我会得到索引,当我写:domain/home/index 我得到
没有路线匹配 [GET]“/home/index”
,都是一样的,我该如何调用我的页面? 耙路线给我:
root / {:controller=>"home", :action=>"index"}
I want to code simple app, with index and about page, without using db, all text will be in views. But I can't call my controller methods, to change view.
Here is controller (home_controller.rb)
class HomeController < ApplicationController
def index
end
def about
end
end
layout:
!!!
%html
%head
%title Title
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tags
%body
.wrapper
= yield
.footer
home index view:
%h2 Index
%h1
This is example page
%p
= link_to "Home", root_path
= link_to "About", :controller => "home", :action => "index"
%p
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
about view:
%h2 About
%h1
This is about page
%p
= link_to "Home", root_path
= link_to "About", :controller => "home", :action => "about"
%p
Lorem ipsum
in routes.rb I have root :to => 'home#index'
when i call my domain, i'll get index, when i write: domain/home/index I get
No route matches [GET] "/home/index"
when I call about, it's the same, how can I call my pages?
rake routes give me:
root / {:controller=>"home", :action=>"index"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也应该定义这些路线。它们不会自动出现。
例如,您可以在默认生成的routes.rb(最后)中找到以下内容。
取消注释,您应该能够访问
/home/index
和/home/about
。You should define those routes as well. They won't appear automatically.
For example, here's what you can find in default generated routes.rb (at the end).
Uncomment this and you should be able to access
/home/index
and/home/about
.尝试将以下内容添加到
root :to => 上方的路由文件中...
行Try adding the following to your routes file above the
root :to => ...
line