I have simple scenario with Articles and Comments. Article has comments.
In my Article show action I display the article and a form to add a comment.
When a comment is submitted it triggers the comments#create action.
When the save is successful it redirects back to the article show action. Which works fine.
My question is when the save is not successful what action should be taken?
Normally I would simply use: "render edit" however in this case I have no comments#edit action.
class ArticlesController < ApplicationController
def show
@article Article.find(params[:id])
@comments = @article.comments.order(created_at: :asc).page(params[:page]).per_page(5)
@comment = Comment.new
end
end
class CommentsController < ApplicationController
def create
@article = Article.find(params[:id])
@comment = @article.comments.new(discussion_params)
@comment.user_id = current_user.id
if @comment.save
redirect_to @article
else
render ???
end
end
private
def discussion_params
params.require(:comment).permit(:content)
end
end
_form.html.erb
<%= form_for([@article, Comment.new], html: { class: "comment-form", id: "comment-form" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :content, placeholder: "Add a comment...", rows: 3, minlength: 10, maxlength: 1000 %>
<button class="btn btn-success" type="submit">Post comment</button>
<% end %>