【问题标题】:rendering a partial with camping and haml用露营和哈姆渲染局部
【发布时间】:2011-01-07 20:26:13
【问题描述】:

我正在尝试使用露营制作一个简单的博客,就像露营附带的示例一样,只是我想将 haml 用于视图而不是 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

【问题讨论】:

  • meta 标签已经在self-closing tags 的列表中,所以你不需要尾随的/;如果你删除它,它有帮助吗?
  • 不,问题在于%body=render @posts 行,但谢谢,我不知道/ 是不必要的..

标签: ruby haml camping


【解决方案1】:

首先,要渲染一个局部,你必须这样做:

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

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

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

【讨论】:

猜你喜欢
  • 2011-07-12
  • 1970-01-01
  • 2012-04-16
  • 2012-05-18
  • 2015-12-16
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多