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