# Routes-problem, probably

**URL:** <https://www.ruby-forum.com/t/routes-problem-probably/129767>\
**Category:** Rails\
**Created:** [February 21, 2008, 11:23am UTC](https://www.ruby-forum.com/t/routes-problem-probably/129767 "2008-02-21T11:23:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 21, 2008, 11:23am UTC](https://www.ruby-forum.com/t/routes-problem-probably/129767/1 "2008-02-21T11:23:12Z")

</div>

Hi there,

I’m still not very good (although I’m getting better) at RoR. I’m trying  
to create a friendship between a user and another user. After using the  
“friendships/new.rhtml”, fill in the user’s name and hit the  
create-button. Now it is suppose to move on to the method “create” but  
it can’t find the method “create” in the friendships controller.

The fault lies either in the “form” of my “new.rhtml” or in my  
routes-file, I’m guessing (since it can’t find a method which exists)…  
But I can’t understand WHY.

It’s probably easy for you to see directly.

####################  
My ROUTES  
####################

ActionController::Routing::Routes.draw do |map|  
map.home ‘’, :controller =\> ‘home’, :action =\>‘index’

# The priority is based upon order of creation: first created -\> highest

priority.

map.resources :users  
map.resource :session  
map.resource :presentation  
map.resources :friendships

# all users

```
map.resources :users do |user|

```

# friendships ‘owned by’ user (user --\> friendship --\> friend)

```
user.resources :friendships, :member => { :accept => :get }
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’

#the default route as the lowest priority.  
map.connect ‘:controller/:action/:id.:format’  
map.connect ‘:controller/:action/:id’

end

#######################  
Friendships Controller  
#######################  
class FriendshipsController \< ApplicationController  
before\_filter :find\_friendships, :only =\> [:index]  
before\_filter :find\_friendship, :only =\> [:show]  
before\_filter :find\_friendship\_auth, :only =\> [:accept, :edit,  
:update, :destroy]

def new  
unless params[:user\_id] and params[:user\_id].to\_i !=  
current\_user.id.to\_i  
@friendship = Friendship.new(:user\_id =\> current\_user.id)  
else  
@friendship = Friendship.new(:user\_id =\> current\_user.id,  
:friend\_id =\> params[:user\_id])  
end  
end

#won’t use the CREATE method, WHY?!!  
def create  
if (@friendship = Friendship.new(params[:friendship]) unless  
Friendship.find\_by\_user\_id\_and\_friend\_id(params[:friendship][:user\_id],  
params[:friendship][:friend\_id]))  
# set datetime  
@friendship.accepted\_at = nil  
if @friendship.save  
put “Friendship was successfully requested”  
redirect\_to friendship\_url(@friendship.friend\_id, @friendship)  
else  
put “Friendship was not successfully requested”  
render :action =\> “new”  
end  
else  
put “Friendship not created (already exists)”  
end  
end

#######################  
new.rhtml (friendships)  
#######################

# Make a friendship request

\<%= error\_messages\_for :friendship %\>

\<% form\_for(:friendship, :url =\> { :controller =\> “friendships”, :action  
=\> “create” }) do |f| %\>

**Friend**  
 \<% unless params[:user\_id] %\> \<%= f.text\_field :friend\_id, :value =\> params[:user\_id] %\> \<% end %\>

\<%= submit\_tag "Confirm Friendship Request" %\>

\<% end %\>

######################  
I would like some sort of feedback to this…probably I’ve just never  
had the pleasure to understand how routes work properly.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 21, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/routes-problem-probably/129767/2 "2008-02-21T15:25:43Z")

</div>

First, you may be having some issues because you’ve got friendships  
defined twice in your routes (one ‘top-level’ and the other nested  
under users). Just a guess, but url\_for might be getting confused.  
Better to used the named route to eliminate confusion.

\<% form\_for(:friendship, :url =\>user\_friendships\_path(@current\_user) )  
do |f| %\>  
…  
\<% end %\>

On Feb 21, 5:23 am, Dag S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 21, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/routes-problem-probably/129767/3 "2008-02-21T17:05:02Z")

</div>

I don’t think it will get confused. But run ‘rake routes’ to see what  
kind of url helpers you can use. It is probably something like AndyV  
mentions.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 22, 2008, 11:34am UTC](https://www.ruby-forum.com/t/routes-problem-probably/129767/4 "2008-02-22T11:34:06Z")

</div>

Okay, thanks for the input and suggestion both of you. I tried using the  
snippet below, but I still recieve the same error “Unknown action, No  
action responded to create”

\<% form\_for(:friendship, :url =\>user\_friendships\_path(@current\_user) )  
do |f| %\>

The first “map.resources :friendships” dosen’t seem to confuse Create  
part of the routes (i recieve the same result with or without), it only  
gives me more possible routes for other stuff related to friendships.

I have a hard time understanding routes, so bare with me. What I could  
understand (and which may be wrong) of the information recieved from the  
“raked routes” is: that the route is incomplete (maybe?)

With my New method which is displayed with the New view (and I believe  
to understand this)  
##########################################################  
new\_user\_friendship GET /users/:user\_id/friendships/new  
{:action=\>“new”, :controller=\>“friendships”}  
##########################################################  
Submitting the form from New it should pass it on to the Create method  
so it will get POSTed. But here is where I get confused. When I raked  
the routes the two Creates can be found without any “home” (like the one  
above). I figure this is what is wrong.  
################################################  
POST /users/:user\_id/friendships  
{:action=\>“create”, :controller=\>“friendships”}  
POST /users/:user\_id/friendships.:format  
{:action=\>“create”, :controller=\>“friendships”}  
################################################

Any suggestions of how to solve it? Am I right about this beeing the  
this which is wrong? Maybe there is more to this which is screwing it  
up?

########################################  
This is the part of the routes that has to do with Friendships…  
########################################

user\_friendships GET /users/:user\_id/friendships  
{:action=\>“index”, :controller=\>“friendships”}

formatted\_user\_friendships GET /users/:user\_id/friendships.:format  
{:action=\>“index”, :controller=\>“friendships”}  
POST /users/:user\_id/friendships  
{:action=\>“create”, :controller=\>“friendships”}  
POST /users/:user\_id/friendships.:format  
{:action=\>“create”, :controller=\>“friendships”}

new\_user\_friendship GET /users/:user\_id/friendships/new  
{:action=\>“new”, :controller=\>“friendships”}

formatted\_new\_user\_friendship GET  
/users/:user\_id/friendships/new.:format {:action=\>“new”,  
:controller=\>“friendships”}

edit\_user\_friendship GET /users/:user\_id/friendships/:id/edit  
{:action=\>“edit”, :controller=\>“friendships”}  
GET /users/:user\_id/friendships/:id;edit  
{:action=\>“edit”, :controller=\>“friendships”}  
GET /users/:user\_id/friendships/:id.:format;edit {:action=\>“edit”,  
:controller=\>“friendships”}

formatted\_edit\_user\_friendship GET  
/users/:user\_id/friendships/:id/edit.:format  
{:action=\>“edit”, :controller=\>“friendships”}

accept\_user\_friendship GET /users/:user\_id/friendships/:id/accept  
{:action=\>“accept”, :controller=\>“friendships”}  
GET /users/:user\_id/friendships/:id;accept  
{:action=\>“accept”, :controller=\>“friendships”}  
GET /users/:user\_id/friendships/:id.:format;accept  
{:action=\>“accept”, :controller=\>“friendships”}

formatted\_accept\_user\_friendship GET  
/users/:user\_id/friendships/:id/accept.:format  
{:action=\>“accept”, :controller=\>“friendships”}

user\_friendship GET /users/:user\_id/friendships/:id  
{:action=\>“show”, :controller=\>“friendships”}

formatted\_user\_friendship GET /users/:user\_id/friendships/:id.:format  
{:action=\>“show”, :controller=\>“friendships”}  
PUT /users/:user\_id/friendships/:id  
{:action=\>“update”, :controller=\>“friendships”}  
PUT /users/:user\_id/friendships/:id.:format  
{:action=\>“update”, :controller=\>“friendships”}  
DELETE /users/:user\_id/friendships/:id  
{:action=\>“destroy”, :controller=\>“friendships”}  
DELETE /users/:user\_id/friendships/:id.:format  
{:action=\>“destroy”, :controller=\>“friendships”}

signup /signup {:action=\>“new”, :controller=\>“users”}  
login /login {:action=\>“new”, :controller=\>“session”}  
logout /logout {:action=\>“destroy”, :controller=\>“session”}  
edit /edit {:action=\>“edit”, :controller=\>“presentation”}  
info /info {:action=\>“info”, :controller=\>“presentation”}  
uploader /uploader {:action=\>“uploader”, :controller=\>“entry”}  
portfolio /portfolio {:action=\>“new”, :controller=\>“portfolio”}  
look /look {:action=\>“look”, :controller=\>“portfolio”}

/:controller/:action/:id.:format  
/:controller/:action/:id

Loaded suite c:/ruby/bin/rake  
Started

Finished in 0.0 seconds.

0 tests, 0 assertions, 0 failures, 0 errors

C:\ruby\authentication\>

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 22, 2008, 12:47pm UTC](https://www.ruby-forum.com/t/routes-problem-probably/129767/5 "2008-02-22T12:47:20Z")

</div>

oh, I forgot…this is what raked routes showed after that.

create\_user\_friendship POST  
/users/:user\_id/friendships/:id/create  
{:action=\>“create”, :controller=\>“friendships”}

POST /users/:user\_id/friendships/:id;create  
{:action=\>“create”, :controller=\>“friendships”}  
POST /users/:user\_id/friendships/:id.:format;create  
{:action=\>“create”, :controller=\>“friendships”}

formatted\_create\_user\_friendship POST  
/users/:user\_id/friendships/:id/create.:format {:action=\>“create”,  
:controller=\>“friendships”}

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 22, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/routes-problem-probably/129767/6 "2008-02-22T16:47:08Z")

</div>

Don’t know WHAT or WHY, but it seems to work now… it’s totally wierd.  
Oh, well, thanks anyways

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 22, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/routes-problem-probably/129767/7 "2008-02-22T12:41:30Z")

</div>

a quick update…I tried to create a “home” for the Create in the routes  
in the  
“user.resources :friendships, :member =\> { :accept =\> :get}”  
by adding the “, :create =\> :post” to the above. But no further  
success…don’t even know if this was right to do.
