露营和haml渲染部分

时间:2011-01-07 20:26:14

标签: ruby haml camping

我正在尝试制作一个带露营的简单博客,就像野营一样,只是我想用haml来代替markaby。我想使用_post.html.haml部分渲染帖子,但我有一种感觉,我可能会采用错误的方式。

Blog.rb

require 'camping'

Camping.goes :Blog

Blogtitle = "My Blog"

module Blog
  # Path to where you want to store the templates
  set :views, File.dirname(__FILE__) + '/views'
  module Blog::Models
    class Post < Base; belongs_to :user; end
    class Comment < Base; belongs_to :user; end
    class User < Base; end
  end

  module Blog::Controllers
    class Index
      def get
        @posts = Post.find :all
        render :index
      end
    end
  end
end

视图/ index.html.haml

!!!
%html
%head
%meta{'http-equiv' => 'Content-Type', :content => 'text/html', :charset => 'UTF-8' }/
%title=Blogtitle
%body=render @posts

视图/ _post.html.haml

%h2=post.title
%p=post.html_body

错误

NoMethodError at /
undefined method `to_sym' for #<Array:0xb6e426d4>

Ruby  (eval): in lookup, line 12
Web  GET 0.0.0.0/

Traceback (innermost first)

(eval): in lookup
(eval): in render
/home/tony/src/blog/views/index.html.haml: in evaluate_source
%body=render @posts

1 个答案:

答案 0 :(得分:6)

首先,要渲染部分内容,您必须执行以下操作:

render :_post, :locals => { :post => post }

如果您想呈现所有帖子,只需使用循环:

%body
  - @posts.each do |post|
    = render :_post, :locals => { :post => post }