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.