ActionController::RoutingError problem

Hi Everyone,

I’ve been teaching myself Ruby on Rails while trying to build a new
website I want to launch in the next few weeks. I’ve gotten pretty
far, but I’ve hit a wall this week and I can’t seem to get around it.
I’ve been trying to implement a friends feature, allowing people to
add, edit and delete friends as they see fit. I’ve been following the
example set in the book “Practical Rails, Social Networking Sites” by
Alan B., and it’s been a big help, but since the particulars of
my application are different, I’ve had to do some adjusting of the
code to make it fit.

Right now, I have no problem logging in as one user, and viewing other
users’ profiles. From there, I can tag a user as a “friend”. I can go
to my friends list, and choose to edit their information, which is
basically their XFN info. However, when I click on “Edit Friendship”,
I get the above error, ActionController::RoutingError. I just can’t
seem to get around it. This is the full error I’m receiving:


ActionController::RoutingError in Friends#edit

Showing app/views/friends/edit.rhtml where line #8 raised:

user_friend_url failed to generate from {:user_id=>#<User:0x2380ef8
@attributes={“last_login_at”=>nil, “authorization_token”=>nil,
“updated_at”=>“2007-12-12 14:44:02”, “zip”=>“000000”,
“last_activity”=>“added a location”,
“hashed_password”=>“06f74df4f98d3b187eaa74a059c7c537d34dc1d1f8bc9cd7a2ae9d8ad7804e2d”,
“last_activity_at”=>“2007-12-12 14:44:02”,
“username”=>“Administrator”, “lng”=>nil, “website”=>“http://
www.example.com”, “enabled”=>“1”, “id”=>“1”, “lat”=>nil,
“created_at”=>“2007-12-06 13:53:35”,
“email”=>“[email protected]”}>, :controller=>“friends”, :action=>“show”,
:friend_id=>#<User:
0x23005f0 @attributes={“last_login_at”=>nil,
“authorization_token”=>nil, “updated_at”=>“2007-12-10 18:41:07”,
“zip”=>“00000”, “last_activity”=>nil,
“hashed_password”=>“5d835050184bcbd2b14c2359bceb6daf42ec10e0123e19c5e9afaeb5cf6e8f27”,
“last_activity_at”=>nil, “username”=>“member”, “lng”=>"-70",
“website”=>“http://www.cnn.org”, “enabled”=>“1”, “id”=>“2”,
“lat”=>“41”, “created_at”=>“2007-12-10 18:41:07”,
“email”=>“[email protected]”}>}, expected:
{:controller=>“friends”, :action=>“show”}, diff: {:user_id=>#<User:
0x2380ef8 @attributes={“last_login_at”=>nil,
“authorization_token”=>nil, “updated_at”=>“2007-12-12 14:44:02”,
“zip”=>“00000”, “last_activity”=>“added a location”,
“hashed_password”=>“06f74df4f98d3b187eaa74a059c7c537d34dc1d1f8bc9cd7a2ae9d8ad7804e2d”,
“last_activity_at”=>“2007-12-12 14:44:02”,
“username”=>“Administrator”, “lng”=>nil, “website”=>“http://
www.example.com”, “enabled”=>“1”, “id”=>“1”, “lat”=>nil,
“created_at”=>“2007-12-06 13:53:35”,
“email”=>“[email protected]”}>, :friend_id=>#<User:0x23005f0
@attributes={“last_login_at”=>nil, “authorization_token”=>nil,
“updated_at”=>“2007-12-10 18:41:07”, “zip”=>“02338”,
“last_activity”=>nil,
“hashed_password”=>“5d835050184bcbd2b14c2359bceb6daf42ec10e0123e19c5e9afaeb5cf6e8f27”,
“last_activity_at”=>nil, “username”=>member", “lng”=>"-70",
“website”=>“http://www.cnn.org”, “enabled”=>“1”, “id”=>“2”,
“lat”=>“41”, “created_at”=>“2007-12-10 18:41:07”,
“email”=>“[email protected]”}>}
Extracted source (around line #8):

5:
6: <% form_for(:friendship,
7: :url => user_friend_path(:user_id => @logged_in_user,
8: :friend_id => @friend),
9: :html => { :multipart => true, :method => :put}) do |f| %>
10:


11: Define your relationship with <%= @friend.username
%>


if I change line 7 and 8 to read @logged_in_user.id and @friend.id,
the error message is much shorter, but still the same:


user_friend_url failed to generate from
{:user_id=>1, :controller=>“friends”, :action=>“show”, :friend_id=>2},
expected: {:controller=>“friends”, :action=>“show”}, diff:
{:user_id=>1, :friend_id=>2}


This is the edit function from my friends_controller:

def edit
@user = User.find(logged_in_user)
@friendship = @user.friendships.find_by_friend_id(params[:id])
@friend = @friendship.friend if @friendship
if !@friendship
redirect_to user_friend_path(:user_id => logged_in_user, :id =>
params[:id])
end
end


in my friendship model file, I have this as well:

belongs_to :user
belongs_to :friend, :class_name => ‘User’, :foreign_key =>
‘friend_id’

in my user’s model, I have this:

has_many :friendships
has_many :friends, :through => :friendships, :class_name => ‘User’

in my routes.rb file, I have this:

map.resources :users, :member => {:enable => :put} do |users|
users.resources :roles
users.resources :friends
users.resources :tags, :name_prefix => ‘user_’,
:controller => ‘user_tags’
end


I did notice, that if in the form_for part of my edit template, I
change the user_friend_path to user_friends_path, the page will load,
but if I go to save the changes, I get this error message:

Unknown action

No action responded to 1

with this link in the address bar:
http://localhost:3000/users/1/friends?friend_id=2

and this in the terminal:

Processing UsersController#1 (for 127.0.0.1 at 2007-12-14 14:28:48)
[PUT]
Session ID: 003df0e09ed24a6fe2e93e946a8ae9e7
Parameters: {“commit”=>“Save”, “friendship”=>{“xfn_met”=>“1”,
“xfn_friendship”=>“xfn_contact”, “xfn_family”=>“xfn_child”,
“xfn_sweetheart”=>“0”, “xfn_geographical”=>“xfn_coresident”,
“xfn_crush”=>“0”, “xfn_date”=>“0”, “xfn_muse”=>“1”,
“xfn_coworker”=>“1”, “xfn_colleague”=>“0”}, “_method”=>“put”,
“action”=>“1”, “id”=>“friends”, “controller”=>“users”,
“friend_id”=>“2”}

ActionController::UnknownAction (No action responded to 1):


now, if above should be edit_friends_path instead of edit_friend_path,
the issue above is that it’s using the user’s controller instead of
the friend’s controller. but I’m not sure how to resolve that.

I know this is long, and I really appreciate any help you could
provide, if you need any more code posted, I’d be happy to. I’ve been
working on this since wednesday, and I just can’t wrap my head around
it.

Here’s my theory:

@friend = @friendship.friend if @friendship

prevents @friend from ever getting set because the @friendship
variable is nil. Then, when you try to create you form with:

6: <% form_for(:friendship,
7: :url => user_friend_path(:user_id =>
@logged_in_user,
8:
:friend_id
=> @friend),
9: :html => { :multipart => true, :method
=> :put}) do |f| %>

it pukes because :friend_id is being set to a nil value. Test it out
by hard-coding a valid friend id in (so you would have :friend_id => 1
instead of :friend_id => @friend). If it works with a valid friend id
there, then my theory was right.

Thanks for the fast idea, I tried changing the edit.rhtml file to
include :friend_id => 2, which is a valid id, but I still get the same
error as before.

I also tried changing the user_friend_path to user_friends_path, but I
still get the same error as above too.

Something that just popped into my mind, the error message above has
all the attributes of the user, not the friend properties. I would
think that since the form is trying to load the friend attributes, but
it has the user attributes that would fail as well. but I could be
wrong :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs