Routes-problem, probably

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.

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. [email protected]

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.

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>

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”}

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

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.