我有一個非常典型的博客網站與職位,其中按日期排列,和信息的網頁,這是由標題任意組織。主要博客部分(posts#index
)呈現正常,但pages#index
似乎是呈現頂層模板(與root
路由相同),就好像match '/info'
規則未被正確檢測到一樣。Rails的控制器渲染錯誤的觀點
據我所知,他們都使用相同的佈局(application.html.haml
)。然而,/posts
路由正確顯示posts/index.html.haml
模板,/pages
路由似乎正在回退,顯示application/index.html.haml
時它應顯示pages/index.html.haml
。
無控制器明確定義index
行動。我不確定這是否是問題的一部分,但爲什麼posts#index
可以正常工作,而pages#index
不會呢?
的routes.rb:
Foo::Application.routes.draw do
root :to => 'application#index'
match '/blog' => 'posts#index'
match '/info' => 'pages#index'
end
控制器:
# application_controller.rb
class ApplicationController < ActionController::Base
def show_tag
@tag = params[:tag]
end
end
# posts_controller.rb
class PostsController < ApplicationController
def show
@date = params[:date]
end
end
# pages_controller.rb
class PagesController < ApplicationController
def show
@title = params[:title]
end
end
...和看法:
# layouts/index.html.haml
!!!
%html
%head
%title Foo
%meta{:charset => "utf-8"}
%meta{"http-equiv" => "X-UA-Compatible", :content => "IE=edge,chrome=1"}
%meta{:name => "viewport", :content => "width=device-width, initial-scale=1, maximum-scale=1"}
= stylesheet_link_tag :application, :media => "all"
= javascript_include_tag :application
= csrf_meta_tags
%body
#container.container
%header
%h1
= link_to("Home", root_path)
#main{:role => "main"}
= yield
%footer
# application/index.html.haml
%p
%a{:href => 'blog'} Blog
%p
%a{:href => 'info'} Info
# posts/index.html.haml
%p List of blog posts
%ul
%li
%a{:href => 'blog/6-1'} June 1st
%li
%a{:href => 'blog/6-2'} June 2nd
# pages/index.html.haml
%p Info about stuff
%ul
%li
%a{:href => 'info/foo'} Foo
%li
%a{:href => 'info/bar'} Bar
我運行的Rails 3.2.13和Ruby 1.9.3 。提前致謝。
關於'應用#index',這將是更傳統的擺在routes.rb中? 在任何情況下,事實上,它是使用相同的** **的佈局是不完全的問題;它比那更細。 :)我編輯帖子(希望)澄清。 – acobster
@acobster我們不把'application#index'放在任何地方。 'routes.rb'中不需要它。您可能需要從路由文件中刪除該文件並重新啓動服務器以查看它是否會更改任何內容。 – Firyn
無關的路由混淆了真正的問題,那就是'views/pages /'目錄不存在! (我正在使用一個保存過濾器將其提交給服務器,之前沒有注意到這一點,我感到很傻) – acobster