Hello! Total newbie question (new to rails, very new to authlogic).
I am attempting to save the user_id to my projects table when a user
is logged in. I have a number of sample projects in my database and I
manually associated the projects to different users. I created a “my
projects” controller that accurately retrieves from the db the
projects associated with the logged in user. The issue I have is
when I create a new project while a user is logged in: The user_id is
not saved to the projects table (the project is saved, just not the
associated user-id). The Read, Update, and Delete functions work fine
from the ‘my projects’ controller.
Below are my models & controllers. I’ve changed the ‘create’ step in
the controller to be a number of things, but nothing seems to work.
Should I be associating ‘projects’ to the ‘UserSession’ instead (model
also included below, but currently empty)? Or do I need to declare
some sort of logic that saves user_id when a user is actually logged
in? Or do I need to create a new route that points to a ‘project save’
that also saves the user_id?
Thanks in advance for input!
--------------------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
------------------User Session model---------------
class UserSession < Authlogic::Session::Base
end
----------------Myprojects controller----------------
class MyprojectController < ApplicationController
def index
@projects = current_user.projects.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @projects }
# render_component :controller => “projectcontroller”, :action
=> “index”, :params => { :projects => @projects }
end
end
def show
@project = current_user.projects.find(params[:id])
respond_to do |format|
#format.html # show.html.erb
# format.xml { render :xml => @myprojects }
format.xml { render( :template=> 'project/show' ) }
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 = 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