我的項目模式是這樣的:如何僅顯示項目/註釋/等。屬於Rails 3中的用戶?
# == 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
項目控制器看起來是這樣的:
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 %>
我不想讓你爲我做這一點,但你能陪我走過了我需要做的。即,是否需要向表中添加新列,或者是否要創建新表並在該新表與我的用戶和項目模型&表之間創建聯接?
要記住的另一件事,我有四個主要模型,我想爲每個用戶個性化。項目,評論,上傳,階段。
呵呵,我有一個超級用戶,我希望能夠查看每個用戶的所有項目/評論/階段/上傳(無論是在粒度級別還是高級別)。
很想聽聽你的想法。
編輯另外,如果你可能指向一些閱讀材料的方向,這可能會幫助我完成整個過程 - 我真的很感激。
人力資源管理模式....我想真正的問題是如何設置USER_ID領域正在創建項目時。例如。根據Rails指南,我應該在項目控制器的create action中執行以下操作:@project = @ user.projects.create(params [:project])''。但是,這給了我一個'NoMethodError'方法'項目'。我意識到user_id字段沒有在創建項目時在數據庫中設置 - 儘管我已經設置了關聯。 – marcamillion 2010-11-22 23:07:41