Souschef
2010-Jan-22 18:10 UTC
Working with a subset (users) data in a common model containing all user data
I have a learning application "Projects" set up with nested "Tasks". I am now trying to use authlogic and set up UI''s so that after a user logs in, the user can go to a UI of "My Projects" and then do all the CRUD they need to do on their projects, but the user will only have Read access to projects that are not theirs (accessible through a different link). All users will have their Projects in the same data model (or that''s what I''m attempting to do). I created a "myprojects" controller, copied over the project controller code & have been trying to work with "current_user.projects", but I''m getting an error since the Project is an undefined method (see line three of Myprojects contoller below for error). How does one do this? Is there a way to pass the Project Controller & Views the "current_user.projects" data to re-use the Project Controller & Views? I know I''ll have to put condition logic in to display or not display the ''Edit'' paths, I''m just buggered on how to pass the subset of data if this is the way to go. Do I need the Myprojects controller or am I barking up a wrong tree? Do named routes come into play here? Enquiring minds want to know as a famous tabloid once advertised... Thanks in advance!! Following is my code: --------------------Project controller-------------------- class ProjectsController < ApplicationController def index @projects = Project.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @projects } end end def show @project = Project.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @projects } end end def new @project = Project.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @project } end end def edit @project = Project.find(params[:id]) end def create @project = Project.new(params[:project]) respond_to do |format| if @project.save flash[:notice] = ''Project was successfully created.'' format.html { redirect_to(@project) } 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 def update @project = Project.find(params[:id]) respond_to do |format| if @project.update_attributes(params[:project]) flash[:notice] = ''Project was successfully updated.'' format.html { redirect_to(@project) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @project.errors, :status => :unprocessable_entity } end end end def destroy @project = Project.find(params[:id]) @project.destroy flash[:notice] = ''Project was successfully deleted.'' respond_to do |format| format.html { redirect_to(projects_url) } format.xml { head :ok } end end end --------------------Myprojects controller-------------------- class MyprojectsController < ApplicationController def index @myprojects = current_user.Project.all #<== The ''Project'' # reference here is getting an undefined method error respond_to do |format| format.html # index.html.erb format.xml { render :xml => @myprojects } end end def show @project = current_user.Project.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @myprojects } end end def new @project = Project.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @project } end end def edit @project = current_user.Project.find(params[:id]) end def create @project = Project.new(params[:project]) respond_to do |format| if @project.save flash[:notice] = ''Project was successfully created.'' format.html { redirect_to(@project) } 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 def update @project = current_user.Project.find(params[:id]) respond_to do |format| if @project.update_attributes(params[:project]) flash[:notice] = ''Project was successfully updated.'' format.html { redirect_to(@project) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @project.errors, :status => :unprocessable_entity } end end end def destroy @project = current_user.Project.find(params[:id]) @project.destroy flash[:notice] = ''Project was successfully deleted.'' respond_to do |format| format.html { redirect_to(myprojects_url) } format.xml { head :ok } end end end --------------------Project model-------------------- class Project < ActiveRecord::Base validates_presence_of :name # allow ordering of tasks by step_number has_many :tasks, :dependent => :destroy, :order => ''step_number ASC'' accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true def task_attributes=(task_attributes) task_attributes.each do |attributes| tasks.build(attributes) end end # Following statements tie Projects to users belongs_to :user end --------------------User model-------------------- class User < ActiveRecord::Base # following line commented out. Came from authlogic, but not sure what it means… # attr_accessible :username, :email, :password # Added following line from railscast demo. Note: http://github.com/binarylogic/authlogic_example # has an optional block for passing other config options, but didn’t go there for now… acts_as_authentic has_many :projects end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.