Routing Error: uninitialized constant UserFriendshipsController - ruby-on-rails

I am working on a Team Treehouse Ruby on Rails app that emulates a basic Facebook app.
I am now added social feature's like friends.
I recently added the following link to one of my views:
<%= link_to "Add Friend", new_user_friendship_path(friend_id: #user), class: 'btn' %>
When click the resulting button, I get the following error:
Routing Error
uninitialized constant UserFriendshipsController
Try running rake routes for more information on available routes.
I am thinking the problem is in either my "user_friendship_controller.rb" of "config/routes.rb " file.
Here is my "user_friendship_controller.rb file:
class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!, only: [:new]
def new
if params[:friend_id]
#friend = User.where(profile_name: params[:friend_id]).first
#user_friendship = current_user.user_friendships.new(friend: #friend)
else
flash[:error] = "Friend required"
end
rescue ActiveRecord::RecordNotFound
render file: 'public/404', status: :not_found
end
end
And here is my "config/routes" file:
Treebook::Application.routes.draw do
as :user do
get '/register', to: 'devise/registrations#new',via: :get, as: :register
get '/login', to: 'devise/sessions#new', via: :get, as: :login
get '/logout', to: 'devise/sessions#destroy', via: :delete, as: :logout
end
devise_for :users, :skip => [:sessions]
as :user do
get '/login' => 'devise/sessions#new', as: :new_user_session
post '/login' => 'devise/sessions#create', as: :user_session
delete '/logout' => 'devise/sessions#destroy', as: :destroy_user_session
end
resources :user_friendships do
end
resources :statuses
get 'feed', to: 'statuses#index', as: :feed
root to: 'statuses#index'
get '/:id', to: 'profiles#show', as: 'profile'
end
Any help figuring out this bug is a great help.
Thanks

It looks like it's just a simple mis-naming of your file.
Try renaming it to user_friendships_controller.rb. Rails expects your class declaration to match the file name (as it stands it'd be looking for you to define UserFriendshipController).

Related

You are being redirected. Rails 5 + Devise

My rails application successfully logs out a user and redirects to the about page when in development mode. But the moment i deploy it to production it returns a 302 status and a page that shows "you are being redirected". Am using devise for authentication below is what my code actually looks like.
The routes
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "users/registrations", confirmations: "users/confirmations", sessions: "devise/sessions" }, skip: [:sessions]
devise_scope :user do
get "login" => "devise/sessions#new", as: :new_user_session
post "login" => "devise/sessions#create", as: :user_session
get "/join" => "users/registrations#new", as: :join
get '/logout', to: "devise/sessions#destroy", as: :destroy_user_session
end
resources :companies
get "/about", to: "pages#about"
get "/faq", to: "pages#faq"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'pages#home'
end
My application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
protected
def after_sign_in_path_for(resource)
company_path(resource)
end
def after_sign_out_path_for(resource_or_scope)
"/about"
end
def store_location
session[:return_to] == request.full_path
end
def clear_stored_location
session[:return_to] = nil
end
def redirect_back_or_to(alternate)
redirect_to(session[:return_to] || alternate)
clear_stored_location
end
end
And finally my link to log out.
<%= link_to "Log out", destroy_user_session_path %>
Please note that i am using devise 4.2.0 and capistrano for deployment.
Regards
~
I had to override devise sessions#controller and changed my routes to:
delete 'logout', to: "users/sessions#destroy", as: :destroy_user_session
Then i added delete method to my link for logging out.

Can't link to another page with user

I want link to another page with my user, but i have an error:
RuntimeError in WelcomeController#edit
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
In my current page
<%= link_to "edit", welcome_edit_path(#user) %>
thi is my controller
def edit
user = User.find_by_id(#user.id)
end
in my page i want link to
<%= user.id %>
I know my problem is my #user was nil, but i dont know how to link to another page with my #user
So, please! help me
this my routes file
get "micropost/new"
get "user/new"
get "user/saved"
get "post/new"
get "post/show"
get "welcome/index"
get "welcome/sucess"
get "welcome/edit"
root :to => "welcome#index"
get '/users/:id', :to => 'welcome#sucess', :as => "user"
match '/relations', to: 'relation#create', via: 'post'
match '/relations/:id', to: 'relation#destroy', via: 'delete'
resources :users
resources :relations, only: [:create, :destroy]
resources :microposts
match '/login', to: 'welcome#create', via: 'post'
match '/logout' => 'welcome#destroy', as: :logout
match '/create', to: 'micropost#create', via: 'post'
match '/signup', to: 'user#signup', via: 'post'
Your code in edit method makes no sense. First thing, you have to define your route properly:
get '/welcome/edit/:id', to: 'welcome#edit', as: 'welcome_edit'
Then, in the WelcomeController:
def edit
#user = User.find(params[:id])
end
and the link to this page:
<%= link_to 'edit', welcome_edit_path(#user) %>

No Route Matches ... Rails Engine

So I keep getting the error:
No route matches {:action=>"create", :controller=>"xaaron/api_keys"}
Which is thrown in the test:
it "should not create an api key for those not logged in" do
post :create
expect(response).to redirect_to xaaron.login_path
end
when I go to spec/dummy and run the rake routes command I see:
api_keys GET /api_keys(.:format) xaaron/api_keys#index
POST /api_keys(.:format) xaaron/api_keys#create
new_api_key GET /api_keys/new(.:format) xaaron/api_keys#new
edit_api_key GET /api_keys/:id/edit(.:format) xaaron/api_keys#edit
api_key GET /api_keys/:id(.:format) xaaron/api_keys#show
PATCH /api_keys/:id(.:format) xaaron/api_keys#update
PUT /api_keys/:id(.:format) xaaron/api_keys#update
DELETE /api_keys/:id(.:format) xaaron/api_keys#destroy
Which shows that yes this route does exist. My routes file for this engine looks like:
Xaaron::Engine.routes.draw do
get 'login' => 'sessions#new', :as => 'login'
get 'logout' => 'sessions#destroy', :as => 'logout'
get 'signup' => 'users#new', :as => 'signup'
get 'permission_denied' => 'error#denied', :as => 'permission_denied'
get 'record_not_found' => 'error#error', :as => 'record_not_found'
get 'password_reset' => 'password_resets#edit', :as => 'rest_user_password'
resource :error, controller: 'error'
resources :users
resources :api_keys
resources :sessions
resources :roles
resources :password_resets
end
What am I missing?
update
For those of you curious how I am getting these routes, its because the dummy app's routes file is set up (by default) as such:
Rails.application.routes.draw do
mount Xaaron::Engine => "/xaaron"
end
Update II
I have been reading this api docs on how routing is done in engines and I believe the way I have done this is correct, how ever the controller is defined as such:
module Xaaron
class ApiKeysController < ActionController::Base
before_action :authenticate_user!
def index
#api_key = Xaaron::ApiKey.where(:user_id => current_user.id)
end
def create
#api_key = Xaaron::ApiKey.new(:user_id => current_user.id, :api_key => SecureRandom.hex(16))
create_api_key(#api_key)
end
def destroy
Xaaron::ApiKey.find(params[:id]).destroy
flash[:notice] = 'Api Key has been deleted.'
redirect_to xarron.api_keys_path
end
end
end
You need to tell your spec you are using the engine routes:
describe ApiKeysController do
routes { Xaaron::Engine.routes }
it "should not create an api key for those not logged in" do
post :create
expect(response).to redirect_to xaaron.login_path
end
end

Rails: Define routes manually after modifying to_param

I have been trying to implement a vanity url for user profiles, based on the example here: Rails 3: Permalink public profile. I have replaced the 'id' with username:
def to_param
username
end
However this has caused issues with my other routes. I have set them so that they match the default sets of routes exactly, when running 'rake routes'.
get '/users/' => 'users#index', :as => :users
post '/users' => 'users#create'
get '/users/new' => 'users#new', :as => :new_user
get '/users/:id/edit' => 'users#edit', :as => :edit_user
patch '/users/:id' => 'users#update'
put '/users/:id' => 'users#update'
delete '/users/:id' => 'users#destroy'
# for vanity url
get '/:id' => 'users#show', :as => :user
With this setup, trying to access delete and update routes give me 'no route matches' error. What is the proper way to specify these, and / or should I be doing this a different way? Any help is appreciated.
I think it's interresting and more readable to keep the resources syntax in the routes.rb, except for the show, which you can rewrite to customize user_path :
resources :users, :except => [:show]
# 2 possibilities for the show url
get '/users/:id' => 'users#show' # can be removed if you don't want to keep /users/:id url
get '/:id' => 'users#show', :as => :user
But change the controller to find user by username instead of id, for example
def show
#post = Post.find_by_username(params[:id]) # instead of Post.find(params[:id])
# ...
end

ruby on rails button_to not activating the delete method

Hi im following the agile web development ebook and i cant seem to activate the logout action
here are the revelant parts (TAB key not working could not format to code)
rake routes
logout DELETE /logout(.:format) sessions#destroy
from the route file
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
my controller
def destroy
session[:user_id] = user.id
redirect_to store_url , notice: "Logged out"
end
and my view (relevant part)
<%= button_to 'Logout', logout_path, method: :delete %>
the error message is
No route matches [GET] "/logout"
i know it should use delete method but nothing i do seems to help
You may need to add a match in your routes. Sorry that I don't have the book with me to refer to.
Put this above your controller :sessions ...
match 'logout' => 'sessions#destroy', :as => :logout
If you didn't put the above line, your logout path should be sessions_logout_path, not logout_path.
Reference:
http://guides.rubyonrails.org/routing.html#naming-routes
match '/logout' => 'sessions#destroy', :via => :delete
or
controller :sessions do
member do
delete :destroy, :as => :logout
end
end

Resources