Rails Nested form validation errors -
i have simple scenario articles , comments. article has comments.
in article show action display article , form add comment.
when comment submitted triggers comments#create action.
when save successful redirects article show action. works fine.
my question when save not successful action should taken?
normally use: "render edit" in case 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" }) |f| %> <%= render 'shared/error_messages', object: f.object %> <%= f.text_area :content, placeholder: "add comment...", rows: 3, minlength: 10, maxlength: 1000 %> <button class="btn btn-success" type="submit">post comment</button> <% end %>
try this.
render 'articles/show'
Comments
Post a Comment