0
votes

Edit: It turns out, this was really an issue of syntax when using render for partials. It should be render partial:


I am following this blog post guide to implement the acts_as_commentable_with_threading gem. http://twocentstudios.com/blog/2012/11/15/simple-ajax-comments-with-rails/

I have a character model with the following comment form partial in the show.html.haml view.

This line is giving me an undefined local variable or method for 'comment'

app/views/characters/show.html.haml

  ...
  = render 'comments/form', :locals => { comment => @new_comment }
  ...

The hash inside the locals hash seemed off to me, so i changed the comment local variable to :comment. This worked fine, but I don't believe that is I am supposed to do.

When I do this, the form partial that is rendered also uses a comment local variable.

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…"

Notice that the object passed into the simple_form_for method is also a local variable. This raised an error too, and i turned it into a symbol as well. Next, the comment.commentable_type raised an error, naturally, because comment was not defined. I cannot turn this into a hash because it is having a method call on it, right?

Whatever the answer is to the question is, it seems like I am going about this the wrong way. I shouldn't be turning things into symbols, when the problem is really that it isn't defined. Where should I define it? Where and how should I define it? I tried simply doing comment in the comments controller,

I am using rails 4.0.0, using simple form, and haml. Could it be a bad syntax for simple form?


EDIT: it was a second line rendering the comment partial that was raising the error. (everything being named comment was making it hard to tell where it was coming from.

  = 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 
1
You forgot to add : in front of comment. Try with this: = render 'comments/form', :locals => { :comment => @new_comment }cristian
Hi thank you for commenting. As explained in the OP, i've tried this. What do you suppose I should do about the subsequent issues that arise? I can't do simple_form_for comment. If I do simple_form_for :comment, then the comment.commentable_type won't work. turning this into a sybmol wont work either.ahnbizcad
:locals => { :comment => @new_comment } is definitely the right syntax, but has @new_comment been defined? If not then in the form the comment local variable will be set to nil.Max Williams
The symbol thing can be little confusing: the reason is that you're setting a variable name and a variable value. The symbol is for the name, and it points to the value to assign to that variable.Max Williams
You could do render @comments and see what partial it is expectingPierre-Louis Gottfrois

1 Answers

1
votes

In your controller:

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

Assuming there is a has_many relation between character and comment.

In your view:

render partial: 'comments/form', locals: { new_comment: @new_comment }

or

render 'comments/form', new_comment: @new_comment

In your partial:

= simple_form_for new_comment, remote: true do |f|