Datamapper、Sinatra、Haml:附加和渲染帖子中的评论
我有一个模型票证,它有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于任何关注或拥有相同内容的人:
这适用于
Sinatra
Haml
等,等等。等人。
For anyone paying attention or having the same:
This works
Sinatra
Haml
etc, etc, et. al.