Rendering parts with campsites and haml

I am trying to create a simple blog with campsites, for example, the example with campsites, only I want to use haml for presentations instead of markaby. I want to display messages using the _post.html.haml part, but I have the feeling that I'm wrong.

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

view / index.html.haml

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

view / _post.html.haml

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

Error

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
+3
source share
1 answer

First of all, to do partial, you will need to do something like this:

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

If you want to display all messages just use a loop:

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

Source: https://habr.com/ru/post/1784164/


All Articles