rails 3, uninitialized constant Tercero::terceroclasificacion - ruby-on-rails-3

i update class_name
/models/tipotercero.rb
class Tipotercero < ActiveRecord::Base
has_many :terceroclasificaciones
has_many :terceros , :class_name => "Terceroclasificacion"
end
/models/tercero.rb
class Tercero < ActiveRecord::Base
has_many :ciudades
has_many :terceroclasificaciones
has_many :tipoterceros, :class_name => "Terceroclasificacion"
end
class Terceroclasificacion < ActiveRecord::Base
belongs_to :tercero
belongs_to :tipotercero
attr_accessor :tercero_id, :tipotercero_id
end
/views/terceros/_form.html.erb
<div class="field">
<% for tipotercero in Tipotercero.all %>
<div>
<%= check_box_tag "tercero[tipotercero_ids][]", tipotercero.id, #tercero.tipoterceros.include?(tipotercero) %>
<%= tipotercero.nombre %>
</div>
<% end %>
</div>
the error is
uninitialized constant Tercero::terceroclasificacion
I have tried to follow the post
Rails 3 has_many :through Form but I could not find the error in my application
add terceros controller
/controllers/terceros_controller.rb
class TercerosController < ApplicationController
# GET /terceros
# GET /terceros.xml
def index
#terceros = Tercero.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #terceros }
end
end
# GET /terceros/1
# GET /terceros/1.xml
def show
#tercero = Tercero.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #tercero }
end
end
# GET /terceros/new
# GET /terceros/new.xml
def new
#tercero = Tercero.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #tercero }
end
end
# GET /terceros/1/edit
def edit
#tercero = Tercero.find(params[:id])
end
# POST /terceros
# POST /terceros.xml
def create
#tercero = Tercero.new(params[:tercero])
# #tercero.attributes = {'tipotercero_ids' => []}.merge(params[:tercero] || {})
respond_to do |format|
if #tercero.save
format.html { redirect_to(#tercero, :notice => 'Tercero was successfully created.') }
format.xml { render :xml => #tercero, :status => :created, :location => #tercero }
else
format.html { render :action => "new" }
format.xml { render :xml => #tercero.errors, :status => :unprocessable_entity }
end
end
end
# PUT /terceros/1
# PUT /terceros/1.xml
def update
#tercero = Tercero.find(params[:id])
respond_to do |format|
if #tercero.update_attributes(params[:tercero])
format.html { redirect_to(#tercero, :notice => 'Tercero was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #tercero.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /terceros/1
# DELETE /terceros/1.xml
def destroy
#tercero = Tercero.find(params[:id])
#tercero.destroy
respond_to do |format|
format.html { redirect_to(terceros_url) }
format.xml { head :ok }
end
end
end
edit 3
the page error is:
NameError in Terceros#new
Showing /home/andres/desarrollos/rubyonrails/proyecto/app/views/terceros/_form.html.erb where line #92 raised:
uninitialized constant Tercero::terceroclasificacion
Extracted source (around line #92):
89: <div class="field">
90: <% for tipotercero in Tipotercero.all %>
91: <div>
92: <%= check_box_tag "tercero[tipotercero_ids][]", tipotercero.id, #tercero.tipoterceros.include?(tipotercero) %>
93: <%= tipotercero.nombre %>
94: </div>
95: <% end %>
Trace of template inclusion: app/views/terceros/new.html.erb
4 edit
error to create tercero is:
NoMethodError in TercerosController#create
undefined method `type_cast' for nil:NilClass
Rails.root: /home/andres/desarrollos/rubyonrails/proyecto
Application Trace | Framework Trace | Full Trace
app/controllers/terceros_controller.rb:44:in `new'
app/controllers/terceros_controller.rb:44:in `create'
Request
Parameters:
{"tercero"=>{"identificacion"=>"1110465574",
"empresa"=>"hogar",
"tipo_identificacion"=>"2",
"direccion1"=>"dierccion",
"nombre"=>"carlos andres",
"direccion2"=>"123",
"ciudad_id"=>"2",
"telefono_fijo"=>"132233",
"telefono_movil"=>"123123",
"fecha_nacimiento(1i)"=>"2013",
"observaciones"=>"",
"fecha_nacimiento(2i)"=>"10",
"fecha_nacimiento(3i)"=>"17",
"representante_legal"=>"",
"tipotercero_ids"=>["1",
"2",
"3"],
"apellido1"=>"colonia",
"apellido2"=>"riveros",
"pagina_web"=>""},
"commit"=>"Crear Tercero",
"authenticity_token"=>"SeUoILctpNr9t6Lx8wSoHVTO5mjk0qJfnzJsb9Jtzao=",
"utf8"=>"✓"}

Sometimes other model cannot be derived from the association name just by Rails conventions and such strange errors occurs. You can pass class name like this:
has_many :terceroclasificaciones, :class_name => "Terceroclasificacion"
And btw, you can make your life easier just by using English names for your models and stuff. You do localization in your view anyways.

Try to remove this
attr_accessor :tercero_id, :tipotercero_id
I think you want it to be an attr_accessible but not attr_accessor

Related

Syntax error, but correct syntax? unexpected ':', expecting ')'

I have a nested form (see below)
Model Artist (artist is a user)
has_many :art_works
has_many :canvases
accepts_nested_attributes_for :art_works //artworks is what im currently working on
accepts_nested_attributes_for :canvases
Controller art_works
def new
#artist = Artist.find(params[:artist_id])
#artwork = #artist.art_works.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #artwork }
end
end
def create
#artwork = ArtWork.new(params[:artwork])
respond_to do |format|
if #artwork.save
format.html { redirect_to #artwork, notice: 'artwork was successfully created.' }
format.json { render json: #artwork, status: :created, location: #artwork }
else
format.html { render action: "new" }
format.json { render json: #artwork.errors, status: :unprocessable_entity }
end
end
end
artworks views _form
<%= form_for(#artwork, :url => artist_art_works_path(current_artist) :multipart => true) do |f| %>
<p>
<%= f.file_field :art %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I was pretty positive that this would work, but i'm assuming my :url is incorrect? I'm not really sure what else it would be though. below are my routes for artworks the reason why I am nesting these things is because an artist can upload art into an artwork model the idea is to have several pieces of art in one artwork (like an album has many images inside of it)
artist_art_works GET /artists/:artist_id/art_works(.:format) art_works#index
POST /artists/:artist_id/art_works(.:format) art_works#create
new_artist_art_work GET /artists/:artist_id/art_works/new(.:format) art_works#new
edit_artist_art_work GET /artists/:artist_id/art_works/:id/edit(.:format) art_works#edit
artist_art_work GET /artists/:artist_id/art_works/:id(.:format) art_works#show
PUT /artists/:artist_id/art_works/:id(.:format) art_works#update
DELETE /artists/:artist_id/art_works/:id(.:format) art_works#destroy
Thank you very much in advance for your help. (sorry for the noobness)
You're missing a comma. Yeah the error message isn't that helpful.
#artwork, :url => artist_art_works_path(current_artist) :multipart => true
vs
#artwork, :url => artist_art_works_path(current_artist), :multipart => true

How get the return value of checkbox in rails

I am sorry because this look like a doubled post, but i saw a lot of other threads and i cant understand anything that i am doing.
I am trying to make an has_and_belongs_to_many but i am stuck.
I managed to make the form display the right information, but i dont know how to save it.
I got:
Orb class:
class Orb < ActiveRecord::Base
attr_accessible :descr, :id, :nome, :orb_type_id, :orbt
validates_presence_of :nome, :orb_type_id
validates :nome, :uniqueness => true
belongs_to :orb_type
has_and_belongs_to_many :books
end
Book class:
class Book < ActiveRecord::Base
attr_accessible :dataf, :datai, :descr, :id, :nome
validates_presence_of :nome
validates :nome, :uniqueness => true
has_and_belongs_to_many :orbs
# allows project page to add items via checkboxes
accepts_nested_attributes_for :orbs
end
A _form:
<% #book.each do |book| %>
<div>
<%= check_box_tag "orb[book_ids][]", book.id, #orb.books.include?(book), id: dom_id(book) %>
<%= book.nome %>
</div>
<% end %>
And the controller:
def new
#book = Book.all
#orb = Orb.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #orb }
end
end
# GET /orbs/1/edit
def edit
#orb = Orb.find(params[:id])
#book = Book.all
end
# POST /orbs
# POST /orbs.json
def create
#orb = Orb.new(params[:orb])
respond_to do |format|
if #orb.save
format.html { redirect_to #orb, notice: 'save was successful' }
format.json { render json: #orb, status: :created, location: #orb }
else
format.html { render action: "Novo" }
format.json { render json: #orb.errors, status: :unprocessable_entity }
end
end
end
# PUT /orbs/1
# PUT /orbs/1.json
def update
params[:orb][:book_ids] ||= []
#orb = Orb.find(params[:id])
respond_to do |format|
if #orb.update_attributes(params[:orb])
format.html { redirect_to #orb, notice: 'save was successful' }
format.json { head :no_content }
else
format.html { render action: "Editar" }
format.json { render json: #orb.errors, status: :unprocessable_entity }
end
end
end
With this, the form has a checkbox with the right values, but it wont be save anywere.
I dont know what i am doing, can some one me explain what i have to do?
You need a join table to use has_and_belongs_to_many
class CreateTableBooksOrbs < ActiveRecord::Migration
def change
create_table :books_orbs, :id => false do |t|
t.references :orb, :null => false
t.references :book, :null => false
end
add_index :books_orbs, [:book_id, :orb_id], :unique => true
end
end
see here my working version : https://github.com/senayar/books

Creating a Friend While On Their Profile Page

I want a User(x) to be able to add another User(y) as a friend while User(x) is on User(y's) Profile Page. I set up a has_many_through and everything works except that I can only add a friend from the User Index View. Thank you in advance...The code is below:
Also:
I wanted to place the "friend" link on the view/profile/show.html.erb. When I added #users = User.all to the existing profiles_controller.rb I received the error - undefined method friendships' for nil:NilClass. When I replaced #user = User.find(params[:id]) with #users = User.all I received the error - NoMethodError in Profiles#show... undefined methodinverse_friends' for nil:NilClass
The Code that works in UserIndexView but not ProfileShowView:
% for user in #users %>
<div class="user">
<p>
<strong><%=h user.email %> <%= user.id %></strong>
<%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post%>
<div class="clear"></div>
</p>
</div>
<% end %>
The following error occurs:
NoMethodError in Profiles#show
Showing /Users/mgoff1/LOAP_1.2.2/app/views/profiles/show.html.erb where line #13 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #13):
10:
11:
12:
13: <% for user in #users %>
14: <div class="user">
15: <p>
16: <strong><%=h user.email %> <%= user.id %></strong>
. . .
app/views/profiles/show.html.erb: 13:in`_app_views_profiles_show_html_erb___2905846706508390660_2152968520'
app/controllers/profiles_controller.rb:19:in `show'
The code to the rest is below.
friendship.rb
class Friendship < ActiveRecord::Base
attr_accessible :create, :destroy, :friend_id, :user_id
belongs_to :user
belongs_to :friend, :class_name => "User"
end
user.rb
class User < ActiveRecord::Base
has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes
# attr_accessible :title, :body
has_one :profile
accepts_nested_attributes_for :profile
before_save do | user |
user.profile = Profile.new unless user.profile
end
end
friendships_controller.rb
class FriendshipsController < ApplicationController
def create
#friendship = current_user.friendships.build(:friend_id => params[:friend_id])
if #friendship.save
flash[:notice] = "Added friend."
redirect_to current_user.profile
else
flash[:error] = "Unable to add friend."
redirect_to root_url
end
end
def destroy
#friendship = current_user.friendships.find(params[:id])
#friendship.destroy
flash[:notice] = "Removed friendship."
redirect_to current_user.profile
end
end
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def index
#users = User.all
end
end
profiles_controller.rb
class ProfilesController < ApplicationController
# GET /profiles
# GET /profiles.json
def index
#profiles = Profile.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #profiles }
end
end
# GET /profiles/1
# GET /profiles/1.json
def show
#user = User.find(params[:id])
#profile = Profile.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #profile }
end
end
# GET /profiles/new
# GET /profiles/new.json
def new
#profile = Profile.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #profile }
end
end
# GET /profiles/1/edit
def edit
#user = User.find(params[:id])
#profile = Profile.find(params[:id])
end
# POST /profiles
# POST /profiles.json
def create
#profile = Profile.new(params[:profile])
respond_to do |format|
if #profile.save
format.html { redirect_to #profile, notice: 'Profile was successfully created.' }
format.json { render json: #profile, status: :created, location: #profile }
else
format.html { render action: "new" }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
# PUT /profiles/1
# PUT /profiles/1.json
def update
#profile = Profile.find(params[:id])
respond_to do |format|
if #profile.update_attributes(params[:profile])
format.html { redirect_to #profile, notice: 'Profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
#profile = Profile.find(params[:id])
#profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url }
format.json { head :no_content }
end
end
end
routes.rb
BaseApp::Application.routes.draw do
resources :friendships
resources :profiles
#get "users/show"
devise_for :users, :controllers => { :registrations => "registrations" }
resources :users
match '/show', to: 'profile#show'
match '/signup', to: 'users#new'
root to: 'static_pages#home'
match '/', to: 'static_pages#home'
. . .
You aren't setting #users in ProfilesController#show.
for object in collection just calls collection.each do |object|, which is why you're getting undefined method 'each' for NilClass (and also why it's generally discouraged to use that syntax, as it creates confusing errors like this one).
profiles_controller.rb
def show
#users = User.all
#...
end
Anytime you try to call methods with no actual object you'll get the 'method undefined'.
It means that the method IS defined - but you have a 'nil' and are trying to call it on that and that method doesn't exists for the 'nil' object.
Please check your actual users table. You'll need users to work with. Please verify that you have some.
If necessary you can create users (at the script/rails console) with
User.new(:name=>'fred', :password =>'pword', :password_confirmation => 'pword' )
You can also place this in your db/seeds.db file so you can run rake db:seed the first time you set the application up on a new machine.

Rails validation count limit on has_many :through

I've got the following models: Team, Member, Assignment, Role
The Team model has_many Members. Each Member has_many roles through assignments. Role assignments are Captain and Runner. I have also installed devise and CanCan using the Member model.
What I need to do is limit each Team to have a max of 1 captain and 5 runners.
I found this example, and it seemed to work after some customization, but on update ('teams/1/members/4/edit'). It doesn't work on create ('teams/1/members/new'). But my other validation (validates :role_ids, :presence => true
) does work on both update and create. Any help would be appreciated.
Update: I've found this example that would seem to be similar to my problem but I can't seem to make it work for my app.
It seems that the root of the problem lies with how the count (or size) is performed before and during validation.
For Example:
When updating a record...
It checks to see how many runners there are on a team and returns a count. (i.e. 5) Then when I select a role(s) to add to the member it takes the known count from the database (i.e. 5) and adds the proposed changes (i.e. 1), and then runs the validation check. (Team.find(self.team_id).members.runner.count > 5) This works fine because it returns a value of 6 and 6 > 5 so the proposed update fails without saving and an error is given.
But when I try to create a new member on the team...
It checks to see how many runners there are on a team and returns a count. (i.e. 5) Then when I select a role(s) to add to the member it takes the known count from the database (i.e. 5) and then runs the validation check WITHOUT factoring in the proposed changes. This doesn't work because it returns a value of 5 known runner and 5 = 5 so the proposed update passes and the new member and role is saved to the database with no error.
Member Model:
class Member < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :password, :password_confirmation, :remember_me
attr_accessible :age, :email, :first_name, :last_name, :sex, :shirt_size, :team_id, :assignments_attributes, :role_ids
belongs_to :team
has_many :assignments, :dependent => :destroy
has_many :roles, through: :assignments
accepts_nested_attributes_for :assignments
scope :runner, joins(:roles).where('roles.title = ?', "Runner")
scope :captain, joins(:roles).where('roles.title = ?', "Captain")
validate :validate_runner_count
validate :validate_captain_count
validates :role_ids, :presence => true
def validate_runner_count
if Team.find(self.team_id).members.runner.count > 5
errors.add(:role_id, 'Error - Max runner limit reached')
end
end
def validate_captain_count
if Team.find(self.team_id).members.captain.count > 1
errors.add(:role_id, 'Error - Max captain limit reached')
end
end
def has_role?(role_sym)
roles.any? { |r| r.title.underscore.to_sym == role_sym }
end
end
Member Controller:
class MembersController < ApplicationController
load_and_authorize_resource :team
load_and_authorize_resource :member, :through => :team
before_filter :get_team
before_filter :initialize_check_boxes, :only => [:create, :update]
def get_team
#team = Team.find(params[:team_id])
end
def index
respond_to do |format|
format.html # index.html.erb
format.json { render json: #members }
end
end
def show
respond_to do |format|
format.html # show.html.erb
format.json { render json: #member }
end
end
def new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #member }
end
end
def edit
end
def create
respond_to do |format|
if #member.save
format.html { redirect_to [#team, #member], notice: 'Member was successfully created.' }
format.json { render json: [#team, #member], status: :created, location: [#team, #member] }
else
format.html { render action: "new" }
format.json { render json: #member.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #member.update_attributes(params[:member])
format.html { redirect_to [#team, #member], notice: 'Member was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #member.errors, status: :unprocessable_entity }
end
end
end
def destroy
#member.destroy
respond_to do |format|
format.html { redirect_to team_members_url }
format.json { head :no_content }
end
end
# Allow empty checkboxes
# http://railscasts.com/episodes/17-habtm-checkboxes
def initialize_check_boxes
params[:member][:role_ids] ||= []
end
end
_Form Partial
<%= form_for [#team, #member], :html => { :class => 'form-horizontal' } do |f| %>
#...
# testing the count...
<ul>
<li>Captain - <%= Team.find(#member.team_id).members.captain.size %></li>
<li>Runner - <%= Team.find(#member.team_id).members.runner.size %></li>
<li>Driver - <%= Team.find(#member.team_id).members.driver.size %></li>
</ul>
<div class="control-group">
<div class="controls">
<%= f.fields_for :roles do %>
<%= hidden_field_tag "member[role_ids][]", nil %>
<% Role.all.each do |role| %>
<%= check_box_tag "member[role_ids][]", role.id, #member.role_ids.include?(role.id), id: dom_id(role) %>
<%= label_tag dom_id(role), role.title %>
<% end %>
<% end %>
</div>
</div>
#...
<% end %>
Try
class Member < ActiveRecord::Base
...
def validate_runner_count
if self.team.members.runner.count > 5
errors.add(:role_id, 'Error - Max runner limit reached')
end
end
def validate_captain_count
if self.team.members.captain.count > 1
errors.add(:role_id, 'Error - Max captain limit reached')
end
end
end

How do I display only the projects/comments/etc. that belong to a user in Rails 3?

My Project Model looks like this:
# == 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
The Projects Controller looks like this:
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
When a user logs in, and goes to clicks 'View all projects' which links to the index action of the projects controller, I want them to only see the projects that they are allowed to see.
The index view of the projects controller looks like this:
<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 %>
I don't want you to do this for me, but could you walk through with me what I would need to do. i.e. do I need to add new columns to the tables, or do I create a new table and create a join between that new table and my users and projects model & tables?
Another thing to keep in mind, I have four major models that I want personalized to each user. Projects, comments, uploads, stages.
Oh, and I have a superuser that I want to be able to view all projects/comments/stages/uploads per user (both on a granular level, and on a high-level).
Would love to hear your thoughts.
Edit: Also, if you could possibly point me in the direction of some reading material that might help me with this entire process - I would really appreciate that.
You need to add in your User model :
class User
...
has_many :projects
end
And this should to the trick :
#projects = current_user.projects
It will work if you have a current_user method of course. Otherwise it's equivalent to (although I'm sure you don't want to pass the user_id) :
#projects = User.find(params[:id]).projects
You should really read the rails tutorial book or the rails guides
def index
#projects = Project.find_by_user_id(HOWEVER YOU KEEP YOUR USER ID)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #projects }
end
end
This work only if user can only see projects they've created them self, as Mike answer.
In case you want a user to be able to access other project then the one he has created, you could use a has_many :through association. Where a table would contain ID of user and ID of project.
You need to add user_id in your project table than you can use above suggestions