我的项目模型如下所示:
# == Schema Information
# Schema version: 20101117094659
#
# Table name: projects
#
# id :integer not null, primary key
# name :string(255)
# description :string(255)
# designer_id :integer
# client_id :integer
# notified :boolean
# created_at :datetime
# updated_at :datetime
# user_id :integer
#
class Project < ActiveRecord::Base
belongs_to :user
has_many :stages
has_many :uploads
has_many :comments
end
Projects Controller看起来像这样:
class ProjectsController < ApplicationController
filter_resource_access
# GET /projects
# GET /projects.xml
def index
@projects = Project.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @projects }
end
end
# GET /projects/1
# GET /projects/1.xml
def show
@project = Project.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @project }
end
end
# GET /projects/new
# GET /projects/new.xml
def new
@project = Project.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @project }
end
end
# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
end
# POST /projects
# POST /projects.xml
def create
@project = Project.new(params[:project])
respond_to do |format|
if @project.save
format.html { redirect_to(@project, :notice => 'Project was successfully created.') }
format.xml { render :xml => @project, :status => :created, :location => @project }
else
format.html { render :action => "new" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
end
end
end
# PUT /projects/1
# PUT /projects/1.xml
def update
@project = Project.find(params[:id])
respond_to do |format|
if @project.update_attributes(params[:project])
format.html { redirect_to(@project, :notice => 'Project was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.xml
def destroy
@project = Project.find(params[:id])
@project.destroy
respond_to do |format|
format.html { redirect_to(projects_url) }
format.xml { head :ok }
end
end
end
当用户登录并转到点击链接到项目控制器的索引操作的“查看所有项目”时,我希望他们只看到允许他们看到的项目。
项目控制器的索引视图如下所示:
<h1>Listing projects</h1>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th></th>
</tr>
<% @projects.each do |project| %>
<tr>
<td><%= link_to project.name, project %> | </td>
<td><%= project.description %> | </td>
<% if permitted_to? :edit, @project %>
<td><%= link_to 'Edit', edit_project_path(project) %></td>
<% end %>
<% if permitted_to? :destroy, @project %>
<td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
</table>
<br />
<% if permitted_to? :create, Project.new %>
<%= link_to 'New Project', new_project_path %>
<% end %>
我不希望你为我这样做,但你可以和我一起走看我需要做的事情。即我是否需要向表中添加新列,或者我是否创建新表并在该新表与我的用户和项目模型之间创建联接。表?
要记住的另一件事是,我有四个主要模型,我想为每个用户个性化。项目,评论,上传,阶段。
哦,我有一个超级用户,我希望能够查看每个用户的所有项目/评论/阶段/上传(在粒度级别和高级别上)。
很想听听你的想法。
编辑:另外,如果你可以指出我可能会帮助我完成整个过程的一些阅读材料 - 我真的很感激。
答案 0 :(得分:2)
您需要添加用户模型:
class User
...
has_many :projects
end
这应该是诀窍:
@projects = current_user.projects
如果你当然有一个current_user方法,那将会有效。否则它相当于(虽然我确定你不想传递user_id):
@projects = User.find(params[:id]).projects
您应该真正阅读rails tutorial book或rails guides
答案 1 :(得分:0)
def index
@projects = Project.find_by_user_id(HOWEVER YOU KEEP YOUR USER ID)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @projects }
end
end
这项工作只有当用户只能看到他们自己创建的项目时,迈克回答。
如果您希望用户能够访问其他项目,那么他可以使用has_many :through association。表中包含用户ID和项目ID。
答案 2 :(得分:0)
您需要在项目表中添加user_id,而不是使用上述建议