I am trying to create a friendship system like facebook. However the
tutorial i am following his from railsspace book and his according to
ruby old. The issue i am currently having his a helper function which
would technically help me define the relationship among the user. Now my
model and controller aren't called the same name per say, and does bring
confusion, but i am only trying it at the moment for testing purpose
Here my Model
class Friendship < ActiveRecord::Base
belongs_to :customer
belongs_to :friend, :class_name => 'Customer', :foreign_key =>
'friend_id'
# approved has the following fields
#
# pending
# requested
# accepted
# declined
#
attr_accessible :approved, :customer_id, :friend_id
#Method
# Return true if the customers are (possibly pending) friends.
def self.exists?(customer, friend)
not find_by_customer_id_and_friend_id(customer, friend).nil?
end
# Record a pending friend request.
def self.request(customer, friend)
unless customer == friend or Friendship.exists?(customer, friend)
transaction do
create(:customer => customer, :friend => friend, :status =>
'pending')
create(:customer => friend, :friend => customer, :status =>
'requested')
end
end
end
end
# Accept a friend request.
def self.accept(customer, friend)
transaction do
accepted_at = Time.now
accept_one_side(customer, friend, accepted_at)
accept_one_side(friend, customer, accepted_at)
end
end
# Delete a friendship or cancel a pending request.
def self.breakup(customer, friend)
transaction do
destroy(find_by_customer_id_and_friend_id(customer, friend))
destroy(find_by_customer_id_and_friend_id(friend, customer))
end
end
private
# Update the db with one side of an accepted friendship request.
def self.accept_one_side(customer, friend, accepted_at)
request = find_by_customer_id_and_friend_id(customer, friend)
request.status = 'accepted'
request.accepted_at = accepted_at
request.save!
end
end
# == Schema Information
#
# Table name: friendships
#
# id :integer not null, primary key
# customer_id :integer
# friend_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# approved :string(255)
#
Here my Controller
class FriendsController < ApplicationController
def index
@customer = Customer.all
end
end
Here my HelpMethod
module FriendsHelper
# Return an appropriate friendship status message.
def friendship_status(customer, friend)
friendship =
Friendship.find_by_customer_id_and_friend_id(customer.id, friend.id)
if friendship.nil?
return "#{friend.first_name} is not your friend (yet)."
end
case friendship.approved
when 'requested' then "#{friend.first_name} would like to be your
friend."
when 'pending' then "You have requested friendship from
#{friend.first_name}."
when 'accepted' then "#{friend.first_name} is your friend."
end
end
end
Here my view
<% @customer.each do |client| %>
<div>
<% unless client.id == current_customer.id %>
<%= client.full_name %>
<%= friendship_status(current_customer, client) %>
<% end %>
</div>
<% end %>
Here the error i am getting
SyntaxError in Friends#index
Showing /home/jean/rail/voyxe/app/views/friends/index.html.erb where
line #5 raised:
app/models/friendship.rb:61: syntax error,
unexpected keyword_end, expecting $end
Extracted source (around line #5):
2: <div>
3: <% unless client.id == current_customer.id %>
4: <%= client.full_name %>
5: <%= friendship_status(current_customer, client) %>
6: <% end %>
7: </div>
8: <% 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 https://groups.google.com/groups/opt_out.