I thought I would include the sessions controller too, just in case it
helps:
# This controller handles the login/logout function of the site.
class SessionsController < ApplicationController
# Be sure to include AuthenticationSystem in Application Controller
instead
# render new.rhtml
def new
end
# render logout.rhtml
def logout
end
def create
self.current_user = User.authenticate(params[:login],
params[:password])
if logged_in?
if params[:remember_me] == "1"
current_user.remember_me unless current_user.remember_token?
cookies[:auth_token] = { :value =>
self.current_user.remember_token , :expires =>
self.current_user.remember_token_expires_at }
end
redirect_back_or_default('/')
flash[:notice] = "Logged in successfully"
else
render :action => 'new'
end
end
def destroy
self.current_user.forget_me if logged_in?
cookies.delete :auth_token
reset_session
flash[:notice] = "You have been logged out."
redirect_back_or_default('/')
end
end
Vince Gilbert wrote:
> Hi,
>
> I am having a really tough time figuring this out. I followed the
> tutorial below to add a RESTful authentication to a Ruby application
> that tracks projects (just a title and a url). The tutorial is for a
> blog, but I just changed blog to projects
>
> http://ruby.about.com/od/rubyonrails/ss/railsblog3.htm
>
> My main table of projects is:
>
> projects
> -------
> ID: integer
> Title: string
> Url: string
>
> The RESTful Authentication plugin adds:
>
> user
> ------
> ID: integer
> login: varchar
> password:varchar
> ....
>
> and a sessions controller.
>
> I would like the application to show the user a list of the projects
> that belong to them when they go do the projects index action. However,
> I have no idea how to link the user ID to a particular project and then
> list their projects based on whether they are the appropriatly logged in
> user.
>
> I figure that when a new project is created, the create method could add
> the user ID to the project in another column. And then, when the
> list/show action is called for the projects, only the appropriate
> projects will show.
>
> Here is the projects controller. Can anyone help me with this? I'm in
> over my head. Thanks, Vince.
>
> class ProjectsController < ApplicationController
> before_filter :login_required
>
> # GET /projects
> # GET /projects.xml
> def index
> @projects = Project.find(: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])
> #[email protected] = @session['user'].id
> 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
>
> # PUT /projects/1
> # PUT /projects/1.xml
> 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
>
> # 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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---