rails app中没有html标签

时间:2012-03-06 19:02:22

标签: ruby-on-rails haml

我是rails的新手,刚创建了一个新项目。我确实在Haml中创建了以下视图,index.html.haml。然而,这是我运行应用程序时获得的来源。我没有得到我在Haml中创建的html或标题标签。第一个是源,第二个是我的Haml文件的内容。

<h2>'All Posts'</h2>
<table id='posts'>
  <thead>
    <tr>
      <th>Post Title</th>
      <th>Post Entry</th>
    </tr>
  </thead>

  <tbody></tbody>
  <tr>
    <td>First Post</td>
    <td>Oylesine bir seyler</td>
  </tr>
</table>

index.html.haml文件:

%h2 'All Posts'

%table#posts
  %thead
    %tr
      %th Post Title
      %th Post Entry

  %tbody
  - @posts.each do |post|
    %tr
      %td= post.title
      %td= post.body

这是我创建的application.html.haml文件:

!!! 5
%html
  %head
    %title Rotten Potatoes!
    = stylesheet_link_tag 'application'
    = javascript_include_tag 'application'
    = csrf_meta_tags

  %body
    = yield

我在这里错过了什么吗?

这是控制器代码:

class MoviesController < ActionController::Base
  def index
    @movies = Movie.all
  end

  def show
    id = params[:id]
    @movie = Movie.find_by_id(id)
  end
end

4 个答案:

答案 0 :(得分:3)

将电影控制器更改为继承自ApplicationController而不是ActionController::Base

class MoviesController < ApplicationController

希望有所帮助。

答案 1 :(得分:1)

删除%html

请参阅thisthis

application.html.haml示例:

!!! 5
-# http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither
-ie_html :class => 'no-js oldie', :lang => 'en' do
  %head
    -# To render a different stylesheet partial inside the head (i.e. for admin layout)
    -# just copy _stylesheets.html.haml, and point to that partial instead.
    = render "layouts/head", :stylesheet_partial => "layouts/stylesheets"

  %body{ :class => "#{controller.controller_name}" }
    #container
      %header#header
        = render "layouts/header"

      #main{ :role => 'main' }
        = render "layouts/flashes"
        = yield

      %footer#footer
        = render "layouts/footer"

    -# Javascript at the bottom for fast page loading
    = render "layouts/javascripts"

答案 2 :(得分:0)

1。)最简单的解决方案是将app.html.haml重命名为app / views / layouts目录中的movies.html.haml,因为Rails首先在app / views / layouts中查找具有相同基本名称的文件作为控制者。

如果没有特定于控制器的布局,Rails应该理论上使用app / views / layouts / application.html.erb或app / views / layouts / application.html.haml。这在Rails 3.2.2中对我不起作用。

2.。)其他可能性是将“应用程序”定义为控制器的布局:

class MoviesController < ActionController::Base
layout "application"

  def index
    @movies = Movie.all
  end

  def show
    id = params[:id]
    @movie = Movie.find_by_id(id)
  end
end

希望有所帮助。

答案 3 :(得分:0)

删除views / layouts / application.html.erb erb超越了application.html.haml