Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Show from two tables not working
    primarykey
    data
    text
    <p>I have two models , <code>Place</code> and <code>Address</code>. And the models relationships are,</p> <p>In <code>Place</code>, <code>has_one :address , :dependent =&gt; :destroy</code> In <code>Address</code>, <code>belongs_to :place</code>.</p> <p>In new form in <code>Places Controller</code> I can add the fields to <code>Address</code> without any problem using <code>fields_for :address</code> in form.</p> <p>But when showing , a <code>Place</code> details, I cannot get the <code>Address</code> details of that place. I don't know where I am doing a mistake.</p> <p>In <code>show.html.erb</code> in <code>Places Controller</code>, I have,</p> <pre><code>&lt;% @places.each do |place| %&gt; &lt;p&gt; &lt;b&gt;Max people:&lt;/b&gt; &lt;%=h place.max_people %&gt; &lt;/p&gt; &lt;p&gt; &lt;b&gt;Street:&lt;/b&gt; &lt;%=h place.address.street %&gt; &lt;/p&gt; &lt;p&gt; City: &lt;/b&gt; &lt;%=h place.address.city %&gt; &lt;/p&gt; &lt;p&gt; Pincode: &lt;/b&gt; &lt;%=h place.pincode %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>So , in <code>PlacesController</code> ,</p> <pre><code>def show @userid=session[:userid] @places = Place.find(:all , :conditions =&gt; ["user_id = ?", @userid]) respond_to do |format| format.html # show.html.erb format.xml { render :xml =&gt; @place } end end </code></pre> <p>which displays all the <code>Place</code> details properly but not <code>Address</code> details. I have searched through SO for finding my mistake but all I found was problem with forms. In my case, new form working properly, but not show.</p> <p>Error is ,</p> <pre><code> NoMethodError in Places#show Showing app/views/places/show.html.erb where line #9 raised: undefined method `street' for nil:NilClass </code></pre> <p>In console,</p> <pre><code> `Place.last.address.street` gives me correct data. </code></pre> <p>Am I missing something? Thanks.</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.
    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