Ruby on rails 在部分rails中显示表两次
rails的新手,我想我可能忽略了一些非常简单的东西,但我在一部分中显示了一个表两次,不确定这是否与我的关联有关 这是属性控制器:Ruby on rails 在部分rails中显示表两次,ruby-on-rails,database,view,associations,partials,Ruby On Rails,Database,View,Associations,Partials,rails的新手,我想我可能忽略了一些非常简单的东西,但我在一部分中显示了一个表两次,不确定这是否与我的关联有关 这是属性控制器: class PropertiesController < ApplicationController before_filter def index @property= Property.all end def new @property = current_user.property.build if signed_i
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
下面是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>
让我知道,如果有任何其他将有助于这个问题。感谢您的所有回复。您在哪里做@property=property。所有您正在设置属性的实例集合类。。。在这个系列中,显然还有更多
当你使用
render @property
它将为集合中名为@property
即使在_属性模板中使用了user.property.each
,也意味着您实际上在说:
对于@property中的每个属性,呈现模板_属性。。。和
每次执行此操作时,都会呈现一个新表,该表为
user.property中的每个属性
如果您只希望一个表,并且在名为@property
的属性列表中,每个单独的“属性”只需要渲染每一行,那么您需要将该表拉到渲染之外
例如:
属性()
名称
地址
和_财产:
<tr>
<td><%= property.name %></td>
<td><%= property.address %></td>
</tr>
在您的用户模型中,您应该有的has-many:properties
而不是的has-many:property
。您的“SQL日志”的第二部分看起来只是显示一个控制台交互。您还打算显示其他内容吗?尝试用图像编辑问题将不允许我显示,因为已将属性更改为属性,仍然存在相同的问题。
<h3>Properties (<%= @user.property.count %>)</h3>
<table>
<tr>
<th>Name</th>
<th>address</th>
</tr>
<%= render @property %>
</table>
<tr>
<td><%= property.name %></td>
<td><%= property.address %></td>
</tr>