Ruby-on-rails - Destroy - Error: No route matches [GET] - ruby-on-rails

I am using destroy
In my view, I have:
#delete_archive_modal.modal.fade
.modal-header
%h3
%i.icon-exclamation-sign
Attention
.modal-body
%p= "Are you sure you want to delete this portal?"
.modal-footer
%a.btn{"data-dismiss" => "modal", :href => "#"} Cancel
= link_to 'Delete', delete_portal_path(portal)
Routes:
resources :portals do
resources :pill_tabs, only: [:show, :edit]
resources :page_urls do
collection do
get :redirects
end
end
resources :zero_touch_configs do
member do
get :history
end
end
member do
get :navigation
get :history
get :sitemap
get :url_list
post :generate_sitemap
post :add_modules
post :archive
post :delete
end
collection do
get :index, path: '/'
get :new, path: '/new(/:portal_type)'
get :accessible_sites
get :archive_index
get :delete
end
And in my controller:
def destroy
#portal = Portal.find(params[:id])
#portal.destroy
flash[:notice] = 'Portal deleted successfully.'
redirect_to action: :archive_index
end
routes:
portals GET /portals(.:format) portals#index
GET /portals/new(/:portal_type)(.:format) portals#new
accessible_sites_portals GET /portals/accessible_sites(.:format) portals#accessible_sites
archive_index_portals GET /portals/archive_index(.:format) portals#archive_index
delete_portals GET /portals/delete(.:format) portals#delete
history_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id/history(.:format) stack_wrappers#history
drafts_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id/drafts(.:format) stack_wrappers#drafts
purge_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id/purge(.:format) stack_wrappers#purge
all_drafts_portal_stack_wrappers GET /portals/:portal_id/stack_wrappers/drafts(.:format) stack_wrappers#all_drafts
portal_stack_wrappers GET /portals/:portal_id/stack_wrappers(.:format) stack_wrappers#index
POST /portals/:portal_id/stack_wrappers(.:format) stack_wrappers#create
new_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/new(.:format) stack_wrappers#new
edit_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id/edit(.:format) stack_wrappers#edit
portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id(.:format) stack_wrappers#show
PATCH /portals/:portal_id/stack_wrappers/:id(.:format) stack_wrappers#update
PUT /portals/:portal_id/stack_wrappers/:id(.:format) stack_wrappers#update
DELETE /portals/:portal_id/stack_wrappers/:id(.:format) stack_wrappers#destroy
history_portal_config_bundle GET /portals/:portal_id/config_bundles/:id/history(.:format) config_bundles#history
portal_config_bundles GET /portals/:portal_id/config_bundles(.:format) config_bundles#index
POST /portals/:portal_id/config_bundles(.:format) config_bundles#create
new_portal_config_bundle GET /portals/:portal_id/config_bundles/new(.:format) config_bundles#new
edit_portal_config_bundle GET /portals/:portal_id/config_bundles/:id/edit(.:format) config_bundles#edit
portal_config_bundle GET /portals/:portal_id/config_bundles/:id(.:format)
But I am getting a routing error and don't know where to go from here...
No route matches [GET] "/portals/asdg/delete"
Can anyone share a guide or point me to documentation that helps me understand what's wrong here?

Try
= link_to 'Delete', portal_path(portal), method: :delete
Here are the all 7 routes for portal
portals GET /portals(.:format) portals#index
POST /portals(.:format) portals#create
new_portal GET /portals/new(.:format) portals#new
edit_portal GET /portals/:id/edit(.:format) portals#edit
portal GET /portals/:id(.:format) portals#show
PATCH /portals/:id(.:format) portals#update
PUT /portals/:id(.:format) portals#update
DELETE /portals/:id(.:format) portals#destroy
And in your routes you have delete_portal path will exist because of
member do
post :delete
end
If you want to call this then you have do define method for this action in your controller and call this path with method: :post
But in RESTful routes delete action have always DELETE method, see in above routes.

There are two issues in your link_to tag.
You used delete_portal_path(portal) but if you run rake routes on your console you will see no such route exist. it have portal_path(portal).
You need to specify method type in route in case of Show(GET default), update(PUT) and delete(DELETE). Because all share same path portal_path(portal).
So your final route should be:
= link_to 'Delete', portal_path(portal), method: :delete
More detail here

You need to define http method when you use delete
= link_to 'Delete', portal_path(portal), method: :delete
It will work.

Related

Rails run :get route within /new and /edit _form

I have a _form for new and edit for a #Giveaway object. Within this form I have a field for a random winner.
I want to populate this field by calling the method giveaways#random_winner with <%= button_to "Randomly Pick Winner!", {:action => 'choose_winner'}, :method => :get %>, but I am getting this error No route matches {:action=>"choose_winner", :controller=>"giveaways"} when loading /giveaways/new.
Here is my controller:
def choose_winner
random_winner = SubscriberUser.where(user_id: current_user.id).pluck(:subscriber_id).sample(1)
session[:random_winner] = random_winner
redirect_to :back
end
Here are the routes that I have tried. I'm not very good at non-scaffold routes yet:
resources :giveaways do
member do
get 'choose_winner' => 'giveaways#choose_winner'
#tried get :choose_winner, as: :choose_winner
#tried get 'new/choose_winner'
#tried get 'choose_winner'
#tried get 'choose_winner', to: 'giveaways#choose_winner', as: 'choose_winner'
end
end
Question -- Why is the page not loading when I have defined the controller and action in the route? Will I have to reload the page when I do run that route... is there a better way to get at this data?
Your routes.rb is close
resources :giveaways do
member do
get :choose_winner
end
end
And then I would use a Rails route helper so you don't have to worry about setting the action/controller yourself.
<%= button_to "Randomly Pick Winner", choose_winner_giveaway_path(#giveaway), method: :get %>

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'

Edit a nested resource in rails

In the following code segment
.comment
%p= comment.comment
%p= comment.user.email
= link_to 'Edit', edit_post_comment_path(comment.post, comment)
= link_to "Delete", [comment.post, comment], method: :delete, data: {confirm: 'Are you sure"'}
why do both Edit and Delete take in comment.post as a parameter? What does it mean?
It require comment.post because you have made nested routes, check your routes.rb file where you have define routes as below:
resources :posts do
resources :comments
end
and your routes is for EDIT and DELETE is
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
That's why you always need to pass comment.post as a parameter.
If you don't want comment.post as parameter you can change your routes as:
resources :posts
resources :comments
OR if you don,t want to pass comment.id in any particular action do your routes as
resources :posts do
resources :comments, :except => [:delete]
end
resources :comments, :only => [:delete]
NOTE: I am assuming that you don't want comment.post parameter for :delete action
Comment is a nested resource here which means comments belongs to a post.
Because rails routes are defined in a RESTFUL Way.
If you see in the RESTful way, all the CRUD operations in comment resource require post resource id, since comment is associated with post.
Not only the 'Edit' and 'Delete' operation requires parent resource id, All the CRUD operation requires it.
Have a look here at Nested resource section.

Routing to show action on logout

I have a logout link that should be routing to users#do_logout but no matter what I do, if I click the link, it routes to the users#show. Here is the code:
Route:
resources :users do
member do
get :profile
post :profile
end
collection do
get "signup", to: 'users#new'
get "login"
post "do_login"
post "do_logout"
end
end
Link:
li = link_to "Sign Out", do_logout_users_path
Users controller action:
def do_logout
session[:user_id] = nil
redirect_to :root
end
Any help would be much appreciated. This is driving me insane.
Your code is not working because, you have set up a POST route for do_logout and your Sign Out link is making a GET request.
To do a POST request from the view, you have to create a form
= form_tag do_logout_users_path do
= submit_tag 'Sign Out'
OR
You could use delete method for this
in routes
delete "do_logout"
and the link
= link_to "Sign Out", do_logout_users_path, :method => :delete

When should I create named routes in Rails?

I'm confused by Rails 3 resource routes. I have following line in my routes.rb
resources :dungeons, only: [ :index, :destroy, :create, :update, :show ]
When I inspect what named routes are create with rake routes, I get:
dungeons GET /dungeons(.:format) dungeons#index
POST /dungeons(.:format) dungeons#create
dungeon GET /dungeons/:id(.:format) dungeons#show
PUT /dungeons/:id(.:format) dungeons#update
DELETE /dungeons/:id(.:format) dungeons#destroy
Why are there only named routes for the routes with a http get method? If I want to create a link to the destroy action, I have to use something like { :action => 'destroy', :method => :delete, :id => dungeon.id } instead of simply destroy_dungeon_path( dungeon ). Is there something wrong with my routes.rb?
Nothing wrong with your routes file. This is the destroy route: dungeon_path(id)
You have to send a DELETE request to trigger it. The show, update and destroy got the same named_route, the only thing what is different is the type of Request (GET for show, PUT for update or DELETE for destroy)
Here everything you need to know routing in Rails3: http://guides.rubyonrails.org/routing.html

Resources