ruby - 用 camping 和 haml 渲染局部

标签 ruby haml camping

我正在尝试制作一个带有露营的简单博客,就像露营附带的示例一样,只是我想使用 haml 作为 View 而不是 markaby。我想使用 _post.html.haml 局部渲染帖子,但我觉得我可能以错误的方式进行处理。

博客.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

views/index.html.haml

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

views/_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

最佳答案

首先,要渲染局部,您必须执行如下操作:

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

如果你想渲染所有的帖子,只需使用一个循环:

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

关于ruby - 用 camping 和 haml 渲染局部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4629962/

相关文章:

ruby - 露营的路径构建方法

ruby-on-rails - 不断接收-SystemStackError : stack level too deep

ruby - ruby-1.9.3-p194 和 ruby​​-1.9.3-p194@global gemsets 有什么区别?

ruby-on-rails - 将条件打开的html元素转换为haml

haml - 有条件地关闭 HAML 中的标签

ruby - Camping 的 URL() 没有按预期给我 "site root"?

ruby - 使用 Pure Ruby 数据库露营

sql - 我如何以编程方式找到这些重复记录?

mysql - rails 服务器命令无法找到库

ruby-on-rails - 在Rails 3引擎中要求Haml的最佳方法