簡體   English   中英

Ruby On Rails“ Hello World”頁面不起作用

[英]Ruby On Rails “Hello World” page doesn't work

我是紅寶石的新手,但我遇到了問題。 當我在瀏覽器中啟動服務器的rails server並轉到頁面www.localhost:3000在ROR默認頁面上一切正常,但是....在使控制器rails generate controller demo index並轉到頁面www.localhost:3000/demo/indexlocalhost:3000/demo相同-沒有任何內容,似乎頁面不存在,只有空白頁面。我嘗試更改index.html.erb代碼<h1>Hello World</h1> -沒變化,我想練習ROR,但是不能因為那個錯誤而做。

工作環境:Windows 7 64位

數據庫:MySQL

您需要具有與操作對應的視圖。

如果您的控制器稱為Demo而您的操作為index ,則應為

# app/controllers/demo_controller.rb
class DemoController < ApplicationController

  def index
  end

end

並且視圖文件應位於app/views/demo/index.html.erb

index動作是控制器的基本動作。 嘗試一下:

http://localhost:3000/demo

訪問索引。

默認情況下,路由/demo/index是具有index ID的show動作。

確保

views / layout / application.html.erb有yield,index.html.erb有一些文本

檢查您的routes.rb

root :to => 'demo#index'

轉到http://localhost:3000/

從您的config / routes.rb文件中取消注釋此行

  match ':controller(/:action(/:id))(.:format)'

public/index.html 刪除 index.html文件

首先,從公用文件夾中刪除或重命名index.html文件。

然后在路由文件中設置root :to => 'demo#index'

然后使用此命令重新啟動服務器。 rails s默認情況下,服務器將在端口3000上運行。

現在只需鍵入http://localhost:3000/http://localhost:3000/demo/index

你刪除了嗎

app_root
--public
----index.html

然而?

Rails將首先在公用文件夾中查找匹配的文件,然后再從您的視圖中構建一個。

如João所示,您還將需要一個視圖。 此外,刪除public/index.html ,最后為您的控制器添加一條路由。

如果您創建了腳手架Rails入門指南,那么您已經在其中找到了路線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM