Note that there are some explanatory texts on larger screens.

plurals
  1. POPolymorphic comments, how to render when comment validation fails?
    primarykey
    data
    text
    <p>I've created an app that has several models (say A, B) that are polymorphically associated with a Comment model. When one views a page associated with A controller, show action, comments associated with the A object are displayed as is a form to create a new object. All of this works and is similar to Ryan Bates' 15 minute blog posted on the rails website. However, if I add validation to ensure that the user does not submit a blank comment I'm unsure how to render that. Here's what I have in my Comments controller:</p> <pre><code>before_filter :load_resources, :only =&gt; [:create] def create if @comment.save redirect_to @back else render @action end end private def load_resources @comment = Comment.new(params[:comment]) case @comment.commentable_type when 'A' @a = A.find(params[:a_id] @comments = @a.comments @back = a_url(@comment.commentable_id) @resource = @a @action = 'as/show' when 'B' ... end end </code></pre> <p>View partial for comments and form (using Haml):</p> <pre><code>=render :partial =&gt; 'comments/comment', :collection =&gt; @comments %h3 Leave a comment: -form_for [@resource, Comment.new] do |f| =f.error_messages =f.hidden_field :commentable_type, :value =&gt; params[:controller].singularize.titleize =f.hidden_field :commentable_id, :value =&gt; params[:id] =f.hidden_field :editor_id, :value =&gt; @current_user.id =f.hidden_field :creator_id, :value =&gt; @current_user.id %fieldset =f.label :subject, 'Subject', :class =&gt; 'block' =f.text_field :subject, :class =&gt; 'block' =f.label :text, 'Comment', :class =&gt; 'block' =f.text_area :text, :class =&gt; 'block' .clear_thick =f.submit 'Submit', :id =&gt; 'submit' </code></pre> <p>What I can seem to figure out is how to deal with validation errors. When validation errors are triggered it doesn't seem to trigger the f.error_messages. Furthermore, when render is triggered it will take the user to a page with the following url: a/2/comments, when I'd like it to render a/2.</p> <p>newest solution:</p> <pre><code>def create subject = "" if !@comment.save subject = "?subject=#{@comment.subject}" end redirect_to @back + subject end </code></pre> <p>Then in the A controller show action:</p> <pre><code>if params.has_key?('subject') @comment = Comment.create(:subject =&gt; params[:subject]) else @comment = Comment.new end </code></pre> <p>This works, but feels kind of ugly...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload