Rails形式的未定义局部变量

恩比斯卡德

编辑:事实证明,这确实是在使用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 
皮埃尔·路易·哥特弗鲁瓦(Pierre-Louis Gottfrois)

在您的控制器中:

def show
  @character = Character.find(params[:id])
  @new_comment = @character.comments.build
end

假设has_many之间存在关系charactercomment

您认为:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Rails 简单形式未定义的局部变量或方法“资源”

在Rails中未定义的局部变量或方法to_a?

Ruby on Rails中的局部变量未定义

Rails:未定义的局部变量或方法`options'

Ruby on Rails呈现部分未定义的局部变量

Rails Ajax未定义的局部变量或方法

rails:未定义的局部变量或方法“请求”

Ruby on Rails的未定义局部变量或方法`profile'

Ruby on Rails错误:未定义的局部变量或方法

人偶-未定义的局部变量

未定义的局部变量或方法

Rails:在循环中渲染局部变量会导致未定义的变量

Rails-未定义的局部变量的Form_for错误?

为什么我得到“未定义的局部变量或方法'rails_env'”?

在本地使用ActiveRecord :: NullMutationTracker:Class的Rails未定义局部变量或方法“ first”

Rails路由错误(未定义的局部变量或方法“ micropost_comment”)

Rails视图中的binding.pry:NameError:未定义的局部变量或方法

Rails:main:Object的未定义局部变量或方法“ signup_path”

Rails-未定义的局部变量或方法“时间表”

Rails 4路由-未定义的局部变量

使用Rails和Devise的未定义局部变量或方法

Rails 4:OldImagePath模块中的未定义局部变量或方法“ asset_paths”

获取未定义的局部变量或方法,以进行表决act_as_votable Rails

Rails教程(M. Hartl)第8章错误:未定义的局部变量

在Rails中使用动态ID填充模式-未定义的局部变量

未定义的局部变量或带有response_with的方法(Ruby on Rails)

NameError:的未定义局部变量或方法“ app”-Rails 5.1.6升级

局部变量未定义的外部方法

未定义的局部变量或方法“sub_task”