0

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 %>
1
  • Can you even post you articles show action? Commented Jul 18, 2015 at 13:03

2 Answers 2

1

Try this.

 render 'articles/show'
Sign up to request clarification or add additional context in comments.

7 Comments

I am guessing that any instance variables I have defined in the articles#show action will need to be duplicated in the comments#create action otherwise they will be unavailable to the view. For example the articles#show action includes @comments which is a list of the existing comments however this is not defined in the comments#create action.
yes you need those instance variables you defined in articles#show incase your comment is not saved. keep that in else condition. sorry i was unaware about the articles show action so i just add the render line as the answer.
Excellent. the page now reloads however the validation errors are not being displayed as expected. I will add my for to the original question.
Validation errors are not being displayed because you are reinitializing comment. Comment.new in the form so the errors will be removed. Initialize @comment = Comment.new in post show action and use that in form for tag. I hope that will work.
You are 100% correct. The reasons I was not initialising @comment in the controller was because it then misrepresents the comment count in my form. When I have an Article with no comments the comment count is correctly displayed as zero however when the save fails and it renders the show action the comment count is displayed as 1. I have asked about this issue previously: stackoverflow.com/questions/31018338/…
|
0

The create action is executed from a new.html page? Then you can use:

render :new

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.