# ActionController::RoutingError problem

**URL:** <https://www.ruby-forum.com/t/actioncontroller-routingerror-problem/123197>\
**Category:** Rails\
**Created:** [December 14, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/actioncontroller-routingerror-problem/123197 "2007-12-14T21:25:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![naevity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9a140/32.png) [@naevity](https://www.ruby-forum.com/u/naevity)\
**Post date:** [December 14, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/actioncontroller-routingerror-problem/123197/1 "2007-12-14T21:25:25Z")

</div>

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](http://www.example.com)”, “enabled”=\>“1”, “id”=\>“1”, “lat”=\>nil,  
“created\_at”=\>“2007-12-06 13:53:35”,  
“email”=\>“removed\_email\_address@domain.invalid”}\>, :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](http://www.cnn.org)”, “enabled”=\>“1”, “id”=\>“2”,  
“lat”=\>“41”, “created\_at”=\>“2007-12-10 18:41:07”,  
“email”=\>“removed\_email\_address@domain.invalid”}\>}, 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](http://www.example.com)”, “enabled”=\>“1”, “id”=\>“1”, “lat”=\>nil,  
“created\_at”=\>“2007-12-06 13:53:35”,  
“email”=\>“removed\_email\_address@domain.invalid”}\>, :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](http://www.cnn.org)”, “enabled”=\>“1”, “id”=\>“2”,  
“lat”=\>“41”, “created\_at”=\>“2007-12-10 18:41:07”,  
“email”=\>“removed\_email\_address@domain.invalid”}\>}  
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](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.

---

<div class="post-metadata">

**Author:** ![naevity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9a140/32.png) [@naevity](https://www.ruby-forum.com/u/naevity)\
**Post date:** [December 14, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/actioncontroller-routingerror-problem/123197/2 "2007-12-14T21:27:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![naevity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9a140/32.png) [@naevity](https://www.ruby-forum.com/u/naevity)\
**Post date:** [December 14, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/actioncontroller-routingerror-problem/123197/3 "2007-12-14T21:36:40Z")

</div>

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 🙂
