对于用户有蓝图的每个项目,我希望显示项目名称和到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
endMy Views/Users/Show.html.erb表
<table>
<tr>
<% @projects.each do |p| %>
<td><%= p.name %></td>
<% end %>
</tr>
</table>发布于 2012-12-09 11:27:48
对于用户有蓝图的每个项目,我希望显示项目名称和到project_path的链接。
对于这些要求,您可以从操作中删除@project分配。您需要查看@user的blueprints关系,并获取每个project的关联blueprints。
<table>
<tbody>
<% @user.blueprints.each do |blueprint| %>
<tr>
<td><%= link_to blueprint.project.name, project_path(blueprint.project) %></td>
</tr>
<% end %>
</tbody>
</table>https://stackoverflow.com/questions/13784154
复制相似问题