Ruby on Rails获得路由

时间:2017-05-14 00:24:08

标签: ruby-on-rails ruby

我对Ruby非常陌生,我需要创建一个动态写入的路径get ="关于我们"

about.html.erb代码:

<h1><%= @title %></h1>

index.html.erb代码:

<h1>Index</h1>

路线代码:

Rails.application.routes.draw do

  root 'posts#index' 

  get 'about' => 'pages#about'

end

在文件夹控制器中,我定义了pages_controller.rbposts_controller.rbpages使用简单函数aboutposts函数index

在文件夹视图/页面中,我有about.html.erb和观看/发布index.html.erb

问题是当我运行它时,它只从索引而不是about.html.erb

写入消息

Rake路线信息:

$ rake routes
   Prefix Verb   URI Pattern               Controller#Action
     root GET    /                         posts#index
    about GET    /about(.:format)          pages#about
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PATCH  /posts/:id(.:format)      posts#update
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy

1 个答案:

答案 0 :(得分:1)

由于您要调用about操作并在routes.rb文件中将其命名为about,因此您必须将浏览器指向:

localhost:3000/about

您还必须将@title变量设置为pages#about操作中的内容。