I'm trying to implement an action which will be invoked when user submits a new comment via ajax. When the comment is saved single comment partial must return. But it seems that render doesn't work as expected from ApplicationController. It displays an error that the view is not found, but it is in place (100%). When I'm adding :partial parameter it works, but doesn't pass any variables (and I need them!). Here's the controller code:
class ApplicationController < ActionController::Base
protect_from_forgery
def comment
comment = Comment.new({
story_id: params[:story_id],
content: params[:content]
})
if comment.save
render 'shared/comments/comment', comment: comment
else
render nothing: true, status: 400
end
end
end
Mischa
43.4k9 gold badges101 silver badges111 bronze badges
asked Mar 30, 2013 at 14:00
Dima Knivets
2,5587 gold badges29 silver badges41 bronze badges
1 Answer 1
Try using this syntax:
render :partial => 'shared/comments/comment', :locals => { :comment => comment }
answered Mar 30, 2013 at 14:05
Marcelo De Polli
29.4k4 gold badges41 silver badges47 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Dima Knivets
Thanks! Yeah, this works, but I've read in documentation that in new versions of Rails, you can just pass your variables without locals array and it works for me in other places, but not here. Why?
Mischa
@DimaKnivets, according to Rails Guides, you should use
object: comment in that case, not comment: comment.Marcelo De Polli
Yes, in this case since the variable has the same name as the partial, you can use
object: comment. But I don't see any mention of this being a preferred option in the docs.default
createaction inside aCommentsController, in keeping with REST practices.