Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Route to destroy action


  • Please log in to reply
6 replies to this topic

#1 starfry

starfry

    Signalman

  • Members
  • 11 posts

Posted 28 November 2013 - 04:17 PM

I have a simple controller for a  session. I have a link to the destroy action in that controller but I needed to create a special route for it. What I have works but I'd like to know if I am doing it the right way. Here's what I have:

 

In a view, there is a link:

<%= link_to "Sign Out", sessions_path, :method => :delete %>

My config/routes.rb contains

resources :sessions

but I had to add an explicit route to get the above link to work:

delete 'sessions' => 'sessions#destroy'

So, am I doing this the right way ?

 

Also, I tried this but it didn't work:

resource  :session, :controller => :sessions, :only => destroy

Any comments/suggestions much appreciated. Thanks.

 



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 28 November 2013 - 06:04 PM

I believe you should be using the singular path helper

<%= link_to "Sign out", session_pah, method: :delete %>

as this will destroy a single session and not destroy all the sessions.


  • Jamie likes this

Blog: http://ohm.sh | Twitter: madsohm


#3 starfry

starfry

    Signalman

  • Members
  • 11 posts

Posted 28 November 2013 - 08:38 PM

@Ohm, yes I thought so too. But when I used "session_path" (along with a tweak of my route to the below), it didn't work.

delete 'session' => 'sessions#destroy'

Doing this gave me an error

No route matches {:controller=>"sessions", :action=>"show"} missing required keys: [:id]

Extracted source 
    <%= link_to "Sign Out", session_path, :method => :delete, id: "sign_out_link" %>


#4 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 28 November 2013 - 08:40 PM

Oh, but of course you'd want to pass along the current session (the one you'd like to destroy)

<%= link_to "Sign Out", session_path(current_session), method: :delete %>

Blog: http://ohm.sh | Twitter: madsohm


#5 starfry

starfry

    Signalman

  • Members
  • 11 posts

Posted 28 November 2013 - 08:50 PM

I don't need to, nor do I want to supply a session id. The sessions controller knows the current session and, when called without an id, it just destroys the current one.

 

It's funny, the routes listed when I made the change I explained in my last post does list what I would expect to be the correct route (the last one in the list) To me, it looks like it should work but it doesn't.

      Prefix Verb   URI Pattern                  Controller#Action
    sessions GET    /sessions(.:format)          sessions#index
             POST   /sessions(.:format)          sessions#create
 new_session GET    /sessions/new(.:format)      sessions#new
edit_session GET    /sessions/:id/edit(.:format) sessions#edit
     session GET    /sessions/:id(.:format)      sessions#show
             PATCH  /sessions/:id(.:format)      sessions#update
             PUT    /sessions/:id(.:format)      sessions#update
             DELETE /sessions/:id(.:format)      sessions#destroy
             GET    /session(.:format)           sessions#show
             DELETE /session(.:format)           sessions#destroy


#6 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 28 November 2013 - 09:00 PM

The standard RESTful way of doing stuff in CRUD is passing the variable, however this is not the way to go with the session (I know).

 

I have the following in one of my apps

# routes.rb
delete 'logout' => 'user_sessions#destroy'

# user_sessions_controller.rb
def destroy
  @user_session = UserSession.find
  if @user_session
    @user_session.destroy
    flash[:information] = t('controllers.user_sessions.logged_out')
  end
  redirect_to root_url
end

# _header.html.erb
<%= link_to t('layouts.header.log_out'), logout_path, method: :delete %>

My UserSession comes from AuthLogic

class UserSession < Authlogic::Session::Base
  ...
end

Blog: http://ohm.sh | Twitter: madsohm


#7 starfry

starfry

    Signalman

  • Members
  • 11 posts

Posted 30 November 2013 - 11:50 PM   Best Answer

The answer was quite simple in the end... Change

resources :sessions
delete 'session' => 'sessions#destroy'
resource  :session, :controller => :sessions, :only => :sho

to

delete 'session' => 'sessions#destroy'
resources :sessions
resource  :session, :controller => :sessions, :only => :show

i.e put the delete route in first.


  • Jamie likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users