I have list with posts (pages#home), on click I open list with comments (posts#show), where I declare variable @feed_items. In the end of the comments I have simple_form for new comment (comments#create). Problem: if error occuring on submit button, I need to render existed list with comments and form with errors.
I’m trying in comments#create:
if @comment.save
...
else
render 'posts/show'
end
but in this case variable @feed_items isn’t declared because method posts#show didn’t called. When I trying to write redirect_to I see list with comments but without error messages. How to do this?
This is a common mistake people do. The
rendermethod only selects the view to display, and theredirect_tomethod executes the action of the redirection again.When there are errors, you call the
rendermethod to select the view you want to display, and you have to manually assign all the instance variables this view need to work out.Check
Section 2.3.2from the rails guides: http://guides.rubyonrails.org/layouts_and_rendering.htmlIn your case it would be something like this: