Datamapper、Sinatra、Haml:附加和渲染帖子中的评论

发布于 2024-09-15 16:23:38 字数 1104 浏览 2 评论 0原文

我有一个模型票证,它有 n 个评论(多对一关系)。

问题是我无法呈现任何评论,表单也无法将评论发布到数据库。

我可以通过 irb 来做这件事。我可以向 Tickets.comments 添加评论,但我无法提取个人评论 - 我可以提取集合,但还不知道如何处理它。它是一个类:DataMapper::Associations::OneToMany::Collection 和文档说“集合在各个方面都应该像数组一样”

所以需要阅读和输入的两个问题:

1)从网络表单发布 2)通过 haml 迭代并渲染集合,我似乎无法做到这一点。

更多血淋淋的细节:

我有一个 Sinatra 方法可以提取

get '/:thisticket' do
   @ticket=Ticket.first(:slug=>params[:slug])
   if @ticket
       haml :showticket

Haml 模板

%div{:class => "ticket"}
 %h1
  = @ticket.slug

= @ticket.comments.all              (returns the # symbol to any html page)

- @ticket.comments.all do |comment|
 %h4
  = comment.a_comment
 %h4
  = comment.created_at
 %h4
  = comment.id                       (this block shows nothing on a rendered page) 

%center
 %form{:action => "/#{@thisticket.slug}/update", :enctype => "text/plain", :method => "post"}
 comments
 %br/
 %textarea{:id => "a_comment",:name => "a_comment", :rows => "5"}
 :preserve
 %br/
 %input{:type => "submit", :value => "post"}/

I have a model Ticket which has n Comments belonging to it (many to one relationship).

The issue is that I can not render any of the comments nor does the form post the comments to the database.

I can kinda sorta do this from irb. I can add comments to tickets.comments, but I cannot pull individual comments - I can pull up the collection but have not figured out how to do anything with it. It is a Class: DataMapper::Associations::OneToMany::Collection and documentation says "A Collection should act like an Array in every way"

So 2 issues needing reading and input:

1) posting from the web form
2) iterating over and rendering the collection through haml, which I can't seem to do.

More gory details:

I have a Sinatra method that pulls up

get '/:thisticket' do
   @ticket=Ticket.first(:slug=>params[:slug])
   if @ticket
       haml :showticket

Haml template

%div{:class => "ticket"}
 %h1
  = @ticket.slug

= @ticket.comments.all              (returns the # symbol to any html page)

- @ticket.comments.all do |comment|
 %h4
  = comment.a_comment
 %h4
  = comment.created_at
 %h4
  = comment.id                       (this block shows nothing on a rendered page) 

%center
 %form{:action => "/#{@thisticket.slug}/update", :enctype => "text/plain", :method => "post"}
 comments
 %br/
 %textarea{:id => "a_comment",:name => "a_comment", :rows => "5"}
 :preserve
 %br/
 %input{:type => "submit", :value => "post"}/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2024-09-22 16:23:38

对于任何关注或拥有相同内容的人:

这适用于

Sinatra

get '/:thisticket' do
   @ticket=Ticket.first(:thisticket=>params[:thisticket])
   @[email protected](:order => [ :created_at.desc ])
   if @ticket
       haml :showticket

Haml

@comments.each do |comment|
comment.comment

等,等等。等人。

For anyone paying attention or having the same:

This works

Sinatra

get '/:thisticket' do
   @ticket=Ticket.first(:thisticket=>params[:thisticket])
   @[email protected](:order => [ :created_at.desc ])
   if @ticket
       haml :showticket

Haml

@comments.each do |comment|
comment.comment

etc, etc, et. al.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文