OK!  Removed restful_authentication & installed/integrated the
authlogic gem.  I have everything up according to railscast and now I
am trying to integrate the authlogic functions.

To be more specific than I originally was, I have 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, then 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.  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.

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   #<-------- '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

==================== END 1/21/10 NOTE==============================

On Jan 15, 8:31 am, Steve Klabnik <[email protected]> wrote:
> Let me get this straight. You have users. Users have Stuff. You want a page,
> say, where a given user can see just their Stuff, and nobody else's?
>
> Well, you'd want to make your User have_many :stuffs, and your Stuffs
> belong_to :user . This'll require a user_id column in your Stuffs table.
>
> Then, finding the Stuff for a certain user is as easy as
> "User.find(id).stuffs" . I forget if restful_authentication has a
> current_user helper, I use AuthLogic, and so I'd just say
> "current_user.stuffs"
>
> But you probably don't want to make a whole second set of controllers just
> for "MyStuffs", unless some stuffs don't belong to a user, but they can
> still modify them. It's hard to understand the relationship with such
> generic terms.
>
> If all stuffs belong to a user, then you probably want to make Stuffs a
> nested resouce of users. See the Railscast on the topic, it might be what
> you're looking for.
>
> http://railscasts.com/episodes/139-nested-resources

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to