I redid my code, before I tried using the friendship-plugin
"has_many_friends" but it got messy and wrong, so I borrowed
"PRACTICAL
RAILS FOR SOCIAL NETWORKING" from the library. I''m trying to use
the
book''s example "ADDING FRIENDS WITH XFN_DETAILS" together
with the
"RESTful authentication"-plugin (which otherwise workes great), but I
can''t seem to get it to work right when implementing friendships.
The book refers to "logged_in_user" to get the USER which is logged
in,
while I refer to "current_user" (from RESTful authentication). I
thought, in this case I only have to change the "logged_in_user" to
"current_user" and that would somewhat be the only change needed from
the books example. This didn''t work. So I tried to implement,
"logged_in_user" (in the lib/ authenticated_system.rb) alongside the
"current_user" but I recieved the same error, which lead me to believe
there is something else which is wrong. It renders an error:
"ActiveRecord::RecordNotFound in FriendsController#new
Couldn''t find User without an ID"
as I try to access
"http://localhost:3000/users/1/friends/new?friend_id=2" to create a
friend
It should (in my humble opinion) find a user - and yes, I''ve got 2
users
in my users table.
I''ve been trying for like two weeks total to understand and implement
different friendship solutions, and I''m beginning to understand (and I
thought I understood today) but I''m sort-of running out of patience....
I''d be so appriciative to anyone who could help me with this.
#########################################################
The development log file...
#########################################################
Processing FriendsController#new (for 127.0.0.1 at 2008-02-27 10:52:14)
[GET]
Session ID: db8f4150f6ee445b8a6cc5c3e1a45a2a
Parameters: {"action"=>"new",
"controller"=>"friends",
"user_id"=>"1",
"friend_id"=>"2"}
[4;36;1mUser Columns (0.000000) [0m [0;1mSHOW FIELDS FROM
users [0m
[4;35;1mUser Load (0.000000) [0m [0mSELECT * FROM users WHERE
(users.`id` = 1) LIMIT 1 [0m
[4;36;1mUser Load (0.000000) [0m [0;1mSELECT * FROM users WHERE
(users.`id` = 1) [0m
ActiveRecord::RecordNotFound (Couldn''t find User without an ID):
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:1012:in
`find_from_ids''
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:419:in
`find''
C:/ruby/authentication/app/controllers/friends_controller.rb:14:in
`new''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/base.rb:1101:in
`send''
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/base.rb:1101:in
##########################################################
FriendsController
##########################################################
class FriendsController < ApplicationController
def index
@user = User.find(params[:user_id], :include => [:friendships =>
:friend])
end
def show
redirect_to user_path(params[:id])
end
def new
@user = User.find(current_user) #example shows "logged_in_user"
instead of current_user
@friend = User.find(params[:firend_id])
unless @user.friends.include?(@friend)
@friendship = @user.friendships.new(:friend_id => @friend.id)
else
redirect_to friend_path(:user_id => current_user, :id => @friend)
#logged_in_user
end
end
def edit
@user = User.find(current_user)#logged_in_user
@friendship = @user.friendships.find_by_friend_id(params[:id])
@friend = @user.firendships.friend if @friendship
if !@friendship
redirect_to friend_path(:user_id => current_user, :id =>
params[:id])#logged_in_user
end
end
def create
@user = User.find(current_user)#logged_in_user
params[:friendship][:friend_id] = params[:friend_id]
@friendship = @user.friendships.create(params[:friendship])
redirect_to friends_path(:user_id => current_user)#logged_in_user
rescue ActiveRecord::RecordInvalid
render :action => ''new''
end
def update
@user = User.find(current_user)#logged_in_user
@friendship = @user.friendships.find_by_friend_id(params[:id])
@friendship.update_attributes(params[:friendship])
redirect_to friends_path(:user_id => current_user)#CURRENT_USER
rescue ActiveRecord::RecordInvalid
render :action => ''edit''
end
def destroy
@user = User.find(params[:user_id])
@friendship = @user.friendships.find_by_friend_id(params[:id]).destroy
redirect_to friends_path(:user => current_user)#CURRENT_USER
end
end
#######################################################################
Friendship Model
#######################################################################
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => ''User'', :foreign_key
=> ''friend_id''
def xfn_friendship=(friendship_type)
self.xfn_friend = false
self.xfn_acquaintance = false
self.xfn_contact = false
self.xfn_classmate = false
case friendship_type
when ''xfn_friend'' :self.xfn_friend = true
when ''xfn_acquaintance'' :self.xfn_acquaintance = true
when ''xfn_contact'' :self.xfn_contact = true
when ''xfn_classmate'' :self.xfn_classmate = true
end
end
def xfn_friendship
return ''xfn_friend'' if self.xfn_friend == true
return ''xfn_acquaintance'' if self.xfn_acquaintance == true
return ''xfn_contact'' if self.xfn_contact == true
return ''xfn_classmatet'' if self.xfn_classmate == true
false
end
def xfn_geographical=(geo_type)
self.xfn_neighbor = false
self.xfn_roomie = false
case geo_type
when ''xfn_neighbor'' :self.xfn_neigbor = true
when ''xfn_roomie'' :self.xfn_roomie = true
end
end
def xfn_geographical
return ''xfn_neighbor''if self.xfn_neighbor == true
return ''xfn_roomie'' if self.xfn_roomie == true
false
end
def xfn_family(family_type) #defining the xnf_family''s family type
which are set to FALSE
self.xfn_parent = false
self.xfn_sibling = false
self.xfn_child = false
self.xfn_spouse = false
case family_type #in a case of
when ''xfn_parent'' :self.xfn_parent = true
when ''xfn_sibling'' :self.xfn_sibling = true
when ''xfn_child'' :self.xfn_child = true
when ''xfn_spouse'' :self.xfn_spouse = true
end
end
def xfn_family
return ''xfn_parent'' if self.xfn_parent == true
return ''xfn_sibling'' if self.xfn_sibling == true
return ''xfn_child'' if self.xfn_child == true
return ''xfn_spouse'' if self.xfn_spouse == true
false
end
###############################################################
new.rhtml
###############################################################
<h2>Add a new friend</h2>
<%= error_messages_for :friendship %>
<% form_for(:friendship,
:url => friends_path(:user_id => @current_user,
:friend_id => @friend),
:html => {:multipart => true}) do |f| %>
<!--@logged_in_user-->
<p>
define your relationship with <strong> <%= friend.login %>
</strong>
</p>
<p>
<p>
<strong>RADIO BUTTONS</strong><br/>
<%= f.radio_button :xfn_friendship, :xfn_contact%> Contact
<%= f.radio_button :xfn_friendship, :xfn_acquaintance%> Acquaintance
<%= f.radio_button :xfn_friendship, :xfn_friend%> Friend
<%= f.radio_button :xfn_friendship, false %> None
</p>
<strong> CHECK BOX </strong><br/>
<%= f.check_box :xfn_met%> Met
</p>
<p>
<strong> CHECKBOX 2 </strong><br/>
<%= f.check_box :xfn_coworker%> Co-worker
<%= f.check_box :xfn_colleague%> Colleague
<%= f.check_box :xfn_classmate%> Classmate
</p>
<p>
<strong> RADIO BUTTONS 2</strong><br/>
<%= f.radio_button :xfn_geographical, :xfn_coresident %> Co-Resident
<%= f.radio_button :xfn_geographical, :xfn_neighbor %> Neighbor
<%= f.radio_button :xfn_geographical, false %> None
</p>
<p>
<strong>RADIO BUTTONS 3</strong>
<%= f.radio_button :xfn_family, :xfn_parent %> Parent
<%= f.radio_button :xfn_family, :xfn_child %> Child
<%= f.radio_button :xfn_family, :xfn_sibling %> Sibling
<%= f.radio_button :xfn_family, :xfn_spouse %> Spouse
<%= f.radio_button :xfn_family, false %> None
</p>
<p>
<strong>CHECK BOX 3</strong>
<%= f.check_box :xfn_muse%> Muse
<%= f.check_box :xfn_crush%> Crush
<%= f.check_box :xfn_date%> Date
<%= f.check_box :xfn_sweetheart%> Sweetheart
</p>
<% f.hidden_field :friend_id %>
<p>
<%= submit_tag ''Save'' %> or <%= link_to
''cancel'', user_path(@friend)%>
</p>
<% end %>
###############################################################
User model (contains more code, but not useful for this)
###############################################################
has_many :friendships
has_many :friends, :through => :friendships, :class_name =>
''User''
###############################################################
Application Helper
###############################################################
module ApplicationHelper
def xfn_rel_tag(user, friendship)
rel_tag = []
if user.id == friendship.friend.id
#identity
rel_tag << ''me''
else
#friendship
rel_tag << ''friend'' if friendship.xfn_friend
rel_tag << ''acquaintance'' if
friendship.xfn_acquaintance
rel_tag << ''contact'' if friendship.xfn_contact
#have they met?
rel_tag << ''met'' if friendship.xfn_met
#professional
rel_tag << ''co-worker'' if friendship.xfn_coworker
rel_tag << ''colleague'' if friendship.xfn_colleague
rel_tag << ''classmate'' if friendship.xfn_classmate
#family?
rel_tag << ''parent'' if friendship.xfn_parent
rel_tag << ''child'' if friendship.xfn_child
rel_tag << ''sibling'' if friendship.xfn_sibling
rel_tag << ''spouse'' if friendship.xfn_spouse
#romantic
rel_tag << ''muse'' if friendship.xfn_muse
rel_tag << ''crush'' if friendship.xfn_crush
rel_tag << ''date'' if friendship.xfn_date
rel_tag << ''sweetheart'' if
friendship.xfn_sweetheart
end
rel_tag.join('' '')
end
end
#####################################################################
sessions controller (some of the code)
#####################################################################
class SessionController < ApplicationController
def create
self.current_user = User.authenticate(params[:login],
params[:password])
if logged_in?
if params[:remember_me] == "1"
self.current_user.remember_me
cookies[:auth_token] = { :value =>
self.current_user.remember_token , :expires =>
self.current_user.remember_token_expires_at }
end
redirect_back_or_default('''')
else
render :action => ''new''
end
end
#####################################################################
Routes-file
#####################################################################
ActionController::Routing::Routes.draw do |map|
map.home '''', :controller => ''home'',
:action =>''index''
map.resource :session
map.resource :presentation
map.resources :users do |users|
users.resources :friends
end
map.signup ''/signup'', :controller =>
''users'', :action => ''new''
map.login ''/login'', :controller =>
''session'', :action => ''new''
map.logout ''/logout'', :controller =>
''session'', :action => ''destroy''
map.edit ''/edit'', :controller =>
''presentation'', :action => ''edit''
map.info ''/info'', :controller =>
''presentation'', :action => ''info''
map.uploader ''/uploader'', :controller =>
''entry'', :action =>
''uploader''
map.portfolio ''/portfolio'', :controller =>
''portfolio'', :action =>
''new''
map.look ''/look'', :controller =>
''portfolio'', :action => ''look''
map.connect '':controller/:action/:id.:format''
map.connect '':controller/:action/:id''
end
###########################################################################
raked routes
###########################################################################
users GET /users
{:action=>"index", :controller=>"users"}
formatted_users GET /users.:format
{:action=>"index", :controller=>"users"}
POST /users
{:action=>"create", :controller=>"users"}
POST /users.:format
{:action=>"create", :controller=>"users"}
new_user GET /users/new
{:action=>"new", :controller=>"users"}
formatted_new_user GET /users/new.:format
{:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit
{:action=>"edit", :controller=>"users"}
GET /users/:id;edit
{:action=>"edit", :controller=>"users"}
GET /users/:id.:format;edit
{:action=>"edit", :controller=>"users"}
formatted_edit_user GET /users/:id/edit.:format
{:action=>"edit", :controller=>"users"}
user GET /users/:id
{:action=>"show", :controller=>"users"}
formatted_user GET /users/:id.:format
{:action=>"show", :controller=>"users"}
PUT /users/:id
{:action=>"update", :controller=>"users"}
PUT /users/:id.:format
{:action=>"update", :controller=>"users"}
DELETE /users/:id
{:action=>"destroy", :controller=>"users"}
DELETE /users/:id.:format
{:action=>"destroy", :controller=>"users"}
user_friends GET /users/:user_id/friends
{:action=>"index", :controller=>"friends"}
formatted_user_friends GET /users/:user_id/friends.:format
{:action=>"index", :controller=>"friends"}
POST /users/:user_id/friends
{:action=>"create", :controller=>"friends"}
POST /users/:user_id/friends.:format
{:action=>"create", :controller=>"friends"}
new_user_friend GET /users/:user_id/friends/new
{:action=>"new", :controller=>"friends"}
formatted_new_user_friend GET /users/:user_id/friends/new.:format
{:action=>"new", :controller=>"friends"}
edit_user_friend GET /users/:user_id/friends/:id/edit
{:action=>"edit", :controller=>"friends"}
GET /users/:user_id/friends/:id;edit
{:action=>"edit", :controller=>"friends"}
GET
/users/:user_id/friends/:id.:format;edit
{:action=>"edit", :controller=>"friends"}
formatted_edit_user_friend GET
/users/:user_id/friends/:id/edit.:format
{:action=>"edit", :controller=>"friends"}
user_friend GET /users/:user_id/friends/:id
{:action=>"show", :controller=>"friends"}
formatted_user_friend GET /users/:user_id/friends/:id.:format
{:action=>"show", :controller=>"friends"}
PUT /users/:user_id/friends/:id
{:action=>"update", :controller=>"friends"}
PUT /users/:user_id/friends/:id.:format
{:action=>"update", :controller=>"friends"}
DELETE /users/:user_id/friends/:id
{:action=>"destroy", :controller=>"friends"}
DELETE /users/:user_id/friends/:id.:format
{:action=>"destroy", :controller=>"friends"}
#####################################################
to be totally clear:
I''ve got a friends-table too containing lost of XFN_attributes and
user_id, friend_id, and NO "id"
--
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---