对于用户拥有蓝图的每个项目,我想显示项目名称和指向 project_path 的链接。谢谢。
这些是我的 ActiveRecords
class User < ActiveRecord::Base
attr_accessible :id, :name
has_many :blueprints
has_many :projects, :through => :blueprints
end
class Project < ActiveRecord::Base
attr_accessible :id, :name
has_many :blueprints
has_many :users, :through => :blueprints
end
class Blueprint < ActiveRecord::Base
attr_accessible :id, :name, :project_id, :user_id
belongs_to :user
belongs_to :project
end
我的用户显示控制器
def show
@user = User.find(params[:id])
@project = Project.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end
我的视图/用户/Show.html.erb 表
<table>
<tr>
<% @projects.each do |p| %>
<td><%= p.name %></td>
<% end %>
</tr>
</table>