Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:50:52+00:00 2026-05-31T19:50:52+00:00

I have a model: review.rb class Review < ActiveRecord::Base validates :title, :presence => true

  • 0

I have a model: review.rb

class Review < ActiveRecord::Base
  validates :title, :presence => true
  belongs_to :product
end

model: product.rb

class Product < ActiveRecord::Base
  validates :name, :presence => true

  has_many :reviews, :dependent => :destroy
end

and this form: _form.html.haml

=form_for([@product, @product.reviews.build]) do |f|
  .field
    =f.label :title
    %br
    =f.text_field :title

with this reviews_controller:

def create
  @product = Product.find(params[:product_id])
  @review = @product.reviews.create(params[:review])
  redirect_to product_path(@product)
end

When I add a review with no title the review doesn’t get created because the title is required (which is good). I’m not sure how to get the errors to show on this association though.

I tried this:

=form_for([@product, @product.reviews.build]) do |f|
  -if @product.reviews.errors.any?
    .errors
      %h2
        =pluralize(@product.reviews.errors.count, "error")
      %ul
        =@product.reviews.errors.full_messages.each do |msg|
          %li
            =msg

but got this error:

undefined method `errors' for #<ActiveRecord::Relation:0x00000103e31df0>

I tried this:

  -if @review.errors.any?
    .errors
      %h2
        =pluralize(@review.errors.count, "error")
      %ul
        =@review.errors.full_messages.each do |msg|
          %li
            =msg

And got:

undefined method `errors' for nil:NilClass

In the controller when I:

    raise @review.errors.inspect

I can see the errors:

#<ActiveModel::Errors:0x00000103d88c28 @base=#<Review id: nil, title: "", description: "", rating: nil, helpful: nil, product_id: 1, created_at: nil, updated_at: nil>, @messages={:title=>["can't be blank", "is too short (minimum is 5 characters)"]}>

What am I forgetting? How do I get the errors to show?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:50:53+00:00Added an answer on May 31, 2026 at 7:50 pm

    When you make the call to redirect_to product_path(@product), you are invoking a different controller action, namely the Products#show action. The instance variables your views will have been reset. You probably aren’t initializing a @review instance variable in this action which is why you are getting an exception.

    What you want is something like this

    # posts_controller.rb
    def show
      @product = Product.find(params[:product_id])
      @review = @product.reviews.build
    end
    
    # show.html.erb
    = form_for([@product, @review]) do |f|
      -if @review.errors.any?
        .errors
          %h2
            =pluralize(@review.errors.count, "error")
          %ul
            =@review.errors.full_messages.each do |msg|
              %li
                =msg
    
    # reviews_controller.rb
    def create
      @product = Product.find(params[:product_id])
      @review = @product.reviews.build(params[:review])
      if @review.save
        redirect_to product_path(@product)
      else
        render 'products/show'
      end
    end
    

    The key is that if the review is unsuccessfully created, you will re-render the page with the same instance variables intact.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Review model, I have the following: class Review < ActiveRecord::Base belongs_to :vendor
I have a model: class Review(models.Model): name = models.CharField(max_length = 50) link = models.CharField(max_length
I have the following models, with their relevant associations: class User < ActiveRecord::Base has_many
I have a model similar to the following: class Review(models.Model): venue = models.ForeignKey(Venue, db_index=True)
I have a join table: class ReviewTag(models.Model): review = models.ForeignKey(Review) tag = models.ForeignKey(Tag) I
My scenario: Movies have reviews, reviews have comments. Movie model: has_many :reviews Review model:
I have model: [XmlRoot(ElementName = event, IsNullable=true)] public class Event { public int id
I have Comment as a polymorphic model. It is attached to Post, Review, etc.
I have model Article it has field title with some text that may contain
Lets say I have model inheritance set up in the way defined below. class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.