0

Newbie to rails, I think i might be overlooking something very simple here, but I am displaying a table twice in a partial, not sure if it's to do with my associations.

Here is the Properties controller:

class PropertiesController < ApplicationController
  before_filter 

  def index
    @property= Property.all
  end

  def new
    @property = current_user.property.build if signed_in? 
  end

  def show
    @property = current_user.property.paginate( params[:page])
  end

Here is the Users Controllers:

class UsersController < ApplicationController
  before_filter :authenticate_user!

  def index
    authorize! :index, @user, :message => 'Not authorized as an administrator.'
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    @property = @user.property.paginate(page: params[:page])
  end

Here are the associations in the models: user model:

class User < ActiveRecord::Base
  has_many :property, dependent: :destroy

property:

class Property < ActiveRecord::Base
  attr_accessible :address, :name
  belongs_to :user 

Here is the _property.html.erb partial

<li>
  <table>                         
    <tr>                          
      <th>Name</th>
      <th>address</th>
    </tr>
    <% @user.property.each do |property| %> 
    <tr>
      <td><%= property.name %></td>  
      <td><%= property.address %></td> 
    </tr>
    <% end %>                        
  </table>
</li>             

Here is the show.html.erb

<div class="row">
   <aside class="span4">
      <section>
         <h1>
           My Properties 
         </h1>
      </section>
   </aside>

   <div class="span8">
     <% if @user.property.any? %>
       <h3>Properties (<%= @user.property.count %>)</h3>
         <ol>
           <%= render @property %>
         </ol>
         <%= will_paginate @property %>
     <% end %>
   </div>
</div>

This is what is rendered in the browser. https://i.stack.imgur.com/TYgHS.png

Let me know if there is anything else will be of help with this question. All responses appreciated.

ksugiarto
  • 940
  • 1
  • 17
  • 40
cyclopse87
  • 619
  • 2
  • 11
  • 22
  • In your User model you should have `has_many :properties` instead of `has_many :property`. – cortex Jul 09 '13 at 00:02
  • The second part of your "SQL log" looks like it's just showing a console interaction. Was there something else you were intending to show? – Peter Alfvin Jul 09 '13 at 00:08
  • Tried to edit the question with an image won't allow me to due to <10 reps. Here is what is rendered in the browser anyway http://i.imgur.com/SlilDo3.png – cyclopse87 Jul 09 '13 at 00:38
  • changed to property to properties, still having the same issue. – cyclopse87 Jul 09 '13 at 00:55

1 Answers1

0

Where you do @property = Property.all you are setting up a collection of instances of the Property class... there's clearly more than on in this collection.

When you use

render @property

it will render the _property template for every item in the collection called @property Even if, inside the _property template you then use user.property.each - then that means that you're effectively saying:

for each property in @property, render the template _property... and each time you do that, render a new table that does a table row for each property in user.property.

if you want only one table, and only each row to be rendered per individual "property" in the list of properties that is called @property then you need to pull the table outside of the render.

eg:

   <h3>Properties (<%= @user.property.count %>)</h3>
       <table>                         
       <tr>                          
         <th>Name</th>
         <th>address</th>
       </tr>
       <%= render @property %>
       </table>

and _property:

<tr>
  <td><%= property.name %></td>  
  <td><%= property.address %></td> 
</tr> 
Taryn East
  • 27,486
  • 9
  • 86
  • 108