2

我想編寫簡單的應用程序,索引和關於頁面,而不使用數據庫,所有的文本將在視圖中。但我無法調用我的控制器方法來更改視圖。 這裏是控制器(home_controller.rb)簡單的Ruby on Rails應用程序

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'

當我把我的域名,我會得到指數,當我寫:域/首頁/指數我得到

沒有路由匹配[GET] 「/首頁/指數」

當我打電話時,它是一樣的,我怎樣才能打電話給我的網頁? 耙路線給我:

根/ {:控制器=> 「家」,:動作=> 「索引」}

回答

2

您應該定義這些路線以及。它們不會自動出現。

例如,以下是默認生成的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

+0

模板丟失 缺少模板首頁/,應用/關於與{:處理器=> [:ERB,:建設者,:咖啡,:haml],:formats => [:html],:locale => [:en,:en]}。搜索:*「/ mnt/_Projects/exapmle2/app/views」wtf?我有這個 – byCoder 2012-02-10 11:56:25

+0

你有'/ app/views/home/about.html.haml'嗎? – 2012-02-10 11:57:30

+0

ofc我有,嗯這很有趣 – byCoder 2012-02-10 12:00:04

1

嘗試添加下列到你的路由文件之上的root :to => ...

get 'home/index' 
get 'home/about' 
+0

您確定要使用反斜槓嗎? :) – 2012-02-10 11:55:18

+0

@Sergio謝謝,最近在窗戶上工作 – 2012-02-10 12:51:26