如果在控制器生成期间跳过,则如何在RoR中创建视图

时间:2012-12-05 04:43:50

标签: ruby-on-rails ruby ruby-on-rails-3

  1. 当我运行时:

    rails generate controller hello index
    

    毫无疑问它会生成hello控制器,但是当我运行这样的另一个命令时不小心:

    rails generate controller world
    

    它成功创建了世界控制器,但错过了Route "world/index",如“hello / index”。对于这个错误,我需要使用destroy controller然后再次生成它,如果忘记某些东西而不是每次都在破坏和创建,我可以生成某种中间命令。

  2. 此命令

    rails generate controller contact-us index
    

    在更改config文件夹下的routes.rb后创建路径contact_us/indexcontact_us。我怎样才能在RoR中创建更友好的SEO?像localhost:3000/contact-us

  3. 我正在制定一些非常基本的规则来跟踪RoR ..比如3个静态页面(主页,关于我们,联系我们)只需要简单的html内容来了解​​更多内容,肯定会为它添加更多功能。

    localhost:3000/home
    localhost:3000/About_us
    localhost:3000/contact_us
    

    我通过创建home,About_us,contact_us controller命令创建了这个,然后在视图中更改了html。由于我处于初始阶段,我在某处查看静态页面,我们可以在公共场合创建它,就像我们在文件夹中有错误页面或者使用的方法是正确的一样?

1 个答案:

答案 0 :(得分:1)

当你使用rails生成器时,它将为它创建控制器和视图文件夹

rails generate controller test

将创建test_controller.rb和view / test文件夹     rails生成控制器测试索引 将创建test_controller.rb和view / test / index.html.erb文件以及为您定义路径

然而,你想要做的事情就是拥有静态页面的单控制器,我建议你做的是生成带有home,aboutus和contact行为的home_controller,然后将路由映射到它们

rails generate controller home

控制器/ home.rb

HomeController < ApplicationController
  def index
  end
  def about_us
  end
  def contact
  end
end

的routes.rb

match '/home', :to => 'home#index'
match '/About_us', :to => 'home#about_us'
match '/Contact_us' , :to=> 'home#contact_us'

并在

中定义您的观点
views/home/index.html.erb
views/home/about_us.html.erb
views/home/contact_us.html.erb