Note that there are some explanatory texts on larger screens.

plurals
  1. POvalidates_presence_of not working properly...how to debug?
    primarykey
    data
    text
    <p>In my Review model, I have the following:</p> <pre><code>class Review &lt; ActiveRecord::Base belongs_to :vendor belongs_to :user has_many :votes validates_presence_of :summary end </code></pre> <p>I submit a new entry as follows in the URL:</p> <pre><code>vendors/9/reviews/new </code></pre> <p>The new.html.erb contains a form as follows:</p> <pre><code>&lt;%= error_messages_for 'review' %&gt; &lt;h1&gt;New review for &lt;%= link_to @vendor.name, @vendor%&gt;&lt;/h1&gt; &lt;% form_for(@review, :url =&gt;vendor_reviews_path(@vendor.id)) do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;p&gt; &lt;%= f.label :summary %&gt;&lt;br /&gt; &lt;%= f.text_area :summary, :rows=&gt;'3', :class=&gt;'input_summary' %&gt; &lt;%= f.hidden_field :vendor_id, :value =&gt; @vendor.id %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.submit 'Submit Review' %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>When I leave the field for :summary blank, I get an error, not a validation message:</p> <p>You have a nil object when you didn't expect it! The error occurred while evaluating nil.name</p> <p>Extracted source (around line #3): </p> <pre><code>1: &lt;%= error_messages_for 'review' %&gt; 2: 3: &lt;h1&gt;New review for &lt;%= link_to @vendor.name, @vendor%&gt;&lt;/h1&gt; </code></pre> <p>I don't understand what is happening, it works if :summary is populated</p> <pre><code> def new @review = Review.new @vendor = Vendor.find(params[:vendor_id]) @review = @vendor.reviews.build respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @review } end end def create @review = Review.new(params[:review]) #@vendor = Vendor.find(params[:vendor_id]) #instantiate the vendor from the URL id -- NOT WOKRING #@review = @vendor.reviews.build #build a review with vendor_id -- NOT working @review = @current_user.reviews.build params[:review]#build a review with the current_user id respond_to do |format| if @review.save flash[:notice] = 'Review was successfully created.' format.html { redirect_to review_path(@review) } format.xml { render :xml =&gt; @review, :status =&gt; :created, :location =&gt; @review } else format.html { redirect_to new_review_path(@review) } format.xml { render :xml =&gt; @review.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>My <em>guess</em> is that when it fails it is going to <code>redirect_to new_review_path(@review)</code> and so doesn't know the vendor it. How can I redirect to <code>vendor/:vendor_id/reviews/new</code> instead?</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.
 

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