2013-07-08 79 views
0

新手導軌,我想我可能忽略了一些非常簡單的東西,但我在部分顯示了一個表格兩次,不確定它是否與我的關聯有關。在部分導軌上顯示錶格兩次

這裏是屬性控制器

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 

這裏是用戶控制器

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 

這裏有關聯的模型: 用戶模型

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

財產

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

這裏是_property.html.erb部分

<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>    

這裏是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> 

這就是在瀏覽器中呈現。 http://i.imgur.com/SlilDo3.png

讓我知道是否還有其他方法可以幫助解決這個問題。所有回覆讚賞。

+0

在您的用戶模型,你應該有'的has_many:properties',而不是'的has_many:property'。 – cortex

+0

「SQL日誌」的第二部分看起來就像只顯示控制檯交互。你有沒有想要展示的東西? –

+0

試圖編輯與圖像的問題不會讓我由於<10代表。以下是瀏覽器中呈現的內容http://i.imgur.com/SlilDo3.png – cyclopse87

回答

0

你在哪裏@property = Property.all你正在設置Property類的實例的集合......在這個集合中顯然比這個更多。

當您使用

render @property 

它將使_property模板稱爲@property 即使,在_property模板裏面,你再使用user.property.each集合中的每一個項目 - 那意味着,你實際上是說:

在@property每個屬性,呈現模板_property ...和 每次你這樣做的時候,呈現一個新的表,做一個錶行的 個user.property中的每個屬性。

如果你想只有一個表,只有每一行

每個單獨的「屬性」,在那個叫@property那麼你需要拉的渲染表之外的屬性列表中呈現。

如:

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

和_property:

<tr> 
    <td><%= property.name %></td> 
    <td><%= property.address %></td> 
</tr> 
+1

現貨上工作,感謝您的努力。 – cyclopse87