ruby on rails button_to not activating the delete method - ruby-on-rails

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

Related

Rails routes based on current user

I followed this rails cast to create authentication for a rails project. My routes currently look like this:
Rails.application.routes.draw do
resources :messages
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
get "new_photo" => "users#edit", :as => "new_photo"
root :to => "users#new"
resources :users
resources :sessions
end
How to I edit this file so that the root will be pointing to "messages#new", if a user is logged in and "users#new" when no user is logged in? I tried many of the solutions on other pages, but they didnt work (they were probably for devise). Thanks for the help!
You'll probably want to handle this in your controller.
routes.rb
root :to => "users#new"
users_controller.rb
def new
return redirect_to new_messages_url if current_user
# normal controller code below...
end
This will redirect the logged_in user (current_user) to the new messages page if already logged in. I'm just assuming that current_user holds your user data, it may be different for your application.
write it in any home controler.
def set_roots
if current_user
redirect_to dashboard_home_index_path
else
redirect_to home_index_path
end
end
in routes.rb file
root :to => 'home#set_roots'
match "/find_roots" => "home#set_roots"

No route matches [GET] "/logout"

In my session controller I have a method called destroy who is pointed in the route as "Logout", the function of it is only reset the session variable to nil and redirect to the store index:
def destroy
session[:user_id] = nil
redirect_to store_url, notice: "Logged out"
end
In the route file I declared the pointer:
get "sessions/destroy"
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
Now in the browser I should trigger the HTTP DELETE VERB, but instead it takes the GET and the route can't be found it.
For example:
localhost:3000/logout, the error is No route matches [GET] "/logout"
In the link you should put explicit method: :destroy, like this:
= link_to 'Destroy', session, method: :destroy
If you look into your Devise initializer in config/initializers/devise.rb and seek through line 236 and switch to :get if you want your users to sign out via a GET request.
config.sign_out_via = :get
I had the same error..
this worked for me.
Just changed the routes from..
delete 'logout', to: 'sessions#destroy'
#to
get 'logout', to: 'sessions#destroy'

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

Routing Error: uninitialized constant UserFriendshipsController

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).

Reusing the name of a custom RESTful route in Rails 3

I want to do something that I imagine looks like this:
resources :users do
collection do
get 'login', :action => 'login_form'
post 'login', :action => 'login'
get 'logout'
end
end
I.e. I want two controller actions to bind to the same path with different methods. How do I do that?
You should read the guide about routes: http://guides.rubyonrails.org/routing.html
resources :users do
collection do
match 'login' => "users#login_form", via: :get
post 'login'
get 'logout'
end
end
A login_form action does not sound very restful. Just saying ;)

Resources