编辑:事实证明,这确实是在使用render
局部词时的语法问题。它应该是render partial:
我正在遵循此博客文章指南来实现act_as_commentable_with_threading gem。http://twocentstudios.com/blog/2012/11/15/simple-ajax-comments-with-rails/
我有一个character
模型,该模型的show.html.haml
视图部分包含以下注释形式。
这行给我一个未定义的局部变量或“注释”方法
app / views / characters / show.html.haml
...
= render 'comments/form', :locals => { comment => @new_comment }
...
对我而言,locals哈希中的哈希似乎不可用,因此我将comment
局部变量更改为:comment
。这个工作很好,但是我不相信那是我应该做的。
当我这样做时,呈现的表单部分也使用comment
局部变量。
app / views / comments / _form.html.haml
.comment-form
= simple_form_for comment, :url => comment_path, :remote => true do |f|
= f.error_notification
= f.input :body, :input_html => { :rows => "3" }, :label => false
= f.input :commentable_type, :as => :hidden, :value => comment.commentable_type
= f.input :commentable_id, :as => :hidden, :value => comment.commentable_id
= f.error :base
= f.button :submit, :class => "btn btn-primary", :disable_with => "Submitting…"
请注意,传递给simple_form_for
方法的对象也是局部变量。这也引发了错误,我也将其转换为符号。接下来,comment.commentable_type
自然会引发一个错误,因为comment
未定义。我不能将其转换为哈希,因为它正在对其进行方法调用,对吗?
无论问题的答案是什么,似乎我都在以错误的方式进行操作。当问题确实是未定义的时候,我不应该将其转换为符号。我应该在哪里定义它?我在哪里以及如何定义它?我只是尝试comment
在comments控制器中进行操作,
我正在使用rails 4.0.0,使用简单的形式和haml。简单形式的语法可能不好吗?
编辑:这是第二行呈现部分注释,这引起了错误。(所有被命名为评论的内容都很难说出它的来源。
= render 'comments/form', comment: @new_comment
= render 'comments/a_comment', collection: @comments, as: :comment
-# partial for a single comment.
%div.comment{ :id => "comment-#{comment.id}" }
%hr
= link_to "×", comment_path(comment), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment?", :disable_with => "×", :class => 'close'
%h4
= comment.user.username
%small= comment.updated_at
%p= comment.body
%h4
在您的控制器中:
def show
@character = Character.find(params[:id])
@new_comment = @character.comments.build
end
假设和has_many
之间存在关系。character
comment
您认为:
render partial: 'comments/form', locals: { new_comment: @new_comment }
或者
render 'comments/form', new_comment: @new_comment
在您的部分中:
= simple_form_for new_comment, remote: true do |f|
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句