简单的 Ruby on Rails 应用程序

发布于 2025-01-04 12:17:32 字数 1567 浏览 1 评论 0原文

我想编写简单的应用程序,带有索引和关于页面,不使用数据库,所有文本都将在视图中。但我无法调用我的控制器方法来更改视图。 这是控制器(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日暮斜阳 2025-01-11 12:17:32

您也应该定义这些路线。它们不会自动出现。

例如,您可以在默认生成的routes.rb(最后)中找到以下内容。

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id(.:format)))'

取消注释,您应该能够访问 /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).

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id(.:format)))'

Uncomment this and you should be able to access /home/index and /home/about.

送舟行 2025-01-11 12:17:32

尝试将以下内容添加到 root :to => 上方的路由文件中...

get 'home/index'
get 'home/about'

Try adding the following to your routes file above the root :to => ... line

get 'home/index'
get 'home/about'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文