Rails not giving a destroy_path for resource - ruby-on-rails

using
Rails 5.1.4 &
ruby 2.4.1
Wondering why Rails would not give a destroy_visitor_path for DELETE request. Any info would be greatly appreciated. Thank you!
routes.rb
Rails.application.routes.draw do
root :to => 'contents#index'
resources :contents
resources :visitors
end
Routes:
visitors GET /visitors(.:format) visitors#index
POST /visitors(.:format) visitors#create
new_visitor GET /visitors/new(.:format) visitors#new
edit_visitor GET /visitors/:id/edit(.:format) visitors#edit
visitor GET /visitors/:id(.:format) visitors#show
PATCH /visitors/:id(.:format) visitors#update
PUT /visitors/:id(.:format) visitors#update
DELETE /visitors/:id(.:format) visitors#destroy

Same reason why there's no update_visitor_path — it's a singular resource that simply responds to a different HTTP verb.
In your view, you'd link_to "Delete", visitor_path, method: :delete
I can't comment on the philosophy behind the design decision, or any of the arguments about whatever spec it's designed to because that's for people who are smarter than I am.
More info on the Rails routing guide: http://guides.rubyonrails.org/routing.html

There are many incidents to delete
It's let you set your own routes:
For example you want to logout after delete (usually in sessions)
get '/logout' => 'sessions#destroy', :as => 'logout'

Related

routing error for root and get pages

I am developing a rails app and This is my first time working with ruby or any server-side programming language. I connected to localhost:3000 and everything went well. I went to change the routing for my home page as well as create some new ones but I'm having an issue of gettign them to preview and I keep Getting error messages. Here is the snippet from my routes.rb file
My routes.rb
Rails.application.routes.draw do
root 'pages#home'
get 'about' => 'pages#about'
post 'forum' => 'pages#forum'
get 'contact-us' => 'pages#contact-us'
end
I'm still relatively new To programming as well as stack overflow so I can't embed images yet So it will only have a link. Any help would be appreciated. Thank you!
1 - Try to avoid fat arrow
2- Controller name should not contain -(dash) instead use _(Underscore) example(contact-us should be contact_us)
Rails.application.routes.draw do
root 'pages#home'
get '/about', to: 'pages#about', as: :about
post '/forum', to: 'pages#forum', as: :forum
get '/contact_us', to: 'pages#contact_us', as: :contact_us
end
3- After changing this run rake routes at your console in your project directory , you will get list of routes
4- you can now use routes helper of prefix_path example (root_path, about_path or forum_path, contact_path)

How to remove /posts/ from URL?

How can I also show a blog post without /posts/ in the URL?
http://www.anthonygalli.com/posts/i-walk-the-line
http://www.anthonygalli.com/posts/50-shades-of-galli
http://www.anthonygalli.com/posts/tips-for-regaining-momentum
Shortened Version (Goal of Question):
http://www.anthonygalli.com/i-walk-the-line
http://www.anthonygalli.com/50-shades-of-galli
http://www.anthonygalli.com/tips-for-regaining-momentum
I have post as a MVC.
routes.rb
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
Change your routes.rb to:
resources :posts, path: '/' do
collection do
get 'about'
get 'feed'
get 'subscribe'
end
end
For documentation on Rails routing check: http://guides.rubyonrails.org/routing.html
Your routes should be updated to look something like this:
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
get ':id' => 'posts#show', :as => :post_by_slug
That as option will be important for linking to your new route if you want to link to the /post/-less paths:
<%= link_to post.title, post_by_slug_path(post) %>
P. S. I believe that the new route will need to be listed last in your routes so that the routes before it can have precedence. (Otherwise, trying to visit /about would try to load a post with a slug named about instead of the posts#about action. You should try this and see if that's true.)
Resource generates the standard CRUD urls, so you have to specify the posts route specifically. Something like this
get '/desired-url' => 'posts#index'

Ruby on Rails Routing changes not working

Recently I've tried to update my routes to be more specific in the routing.rb file:
resources :users
match '/signup', :to => 'users#new'
I removed the resources :users and changed the above to
match 'users/new' => 'users#new'
match 'users/show/:id' => 'users#show', :as => :users_show
match 'users/edit' => 'users#edit'
match '/signup', :to => 'users#new'
But when I try to click on the same link that I've had before in my layout page using the signup_path (http://localhost:3000/signup), I get the following error:
undefined method `users_path' but I am not using users_path anywhere
Shouldn't it still work? I haven't changed anything else in the other pages. The controller and actions are still the same.
Thanks!
What it tells is that you don have helper method named "users_path", which is created by resources in routes.rb.
Check by yourself with rake routes, what you get with your example:
users_new /users/new.(:format) {:controller=>"users", :action=>"new"}
users_show /users/show/:id(.:format) {:controller=>"users", :action=>"show"}
users_edit /users/edit(.:format) {:controller=>"users", :action=>"edit"}
(and no, you don't need forward slash at the beginning of path).
You defined only routes for some pages (show/new/edit), but you don't have path for index/create/update/destroy actions. Add resource back to routes.rb and check which routes it generates. Remember that by default your match-ed routes match all requests, not specific HTTP methods (like with resource). If you want to fully reassemble this behavior, take a look at documentation (at http://api.rubyonrails.org search for "match") and :via parameter.
Also why do you want to remove resource, and add all paths by yourself? If you want to limit available routes, then there are :only/:except options, you can also pass block and add routes by yourself, just check documentation (at http://api.rubyonrails.org and search for "resources" - it's shame I no longer can get link to specific documentation page and from Rails documentation).
I think basicxman is on the right track. You need a leading '/' in all your routes
Change:
match 'users/new' => 'users#new'
match 'users/show/:id' => 'users#show', :as => :users_show
match 'users/edit' => 'users#edit'
To:
match '/users/new' => 'users#new'
match '/users/show/:id' => 'users#show', :as => :users_show
match '/users/edit' => 'users#edit'
Also, do you still have the resources :users line at the top of your routes.rb? That'll also be necessary.
You have to add a route to create action as well. Probably your signup form points to users_path with POST method which is basically the create action. So add to your routes the following
match 'users' => 'users#create', :via => :post, :as=>:users
For more customization info please see the Rails routing guide

Rails 3 route appends _index to route name

I am migrating a Rails 2.3.8 version to Rails 3.0 and so ive rewritten my routes file. When i list the routes using rake routes, i see some route names have _index appended to them. I cant figure out why this is.
Relevant routes:
Rails 2.3.8:
map.namespace "tracker", :path_prefix => "" do |planner|
planner.resources :planner, :collection => {:step1 => :get,
:add => :get,
:unsubscribe => [:get, :post] }
end
Rails 3.0 route:
namespace "tracker", :path => "" do
resources :planner do
collection do
get :step1
get :add
get :unsubscribe
post :unsubscribe
end
end
end
Output from rake routes
Rails 2.3.8
step1_tracker_planner GET /planner/step1(.:format)
add_tracker_planner GET /planner/add(.:format)
unsubscribe_tracker_planner GET /planner/unsubscribe(.:format)
POST /planner/unsubscribe(.:format)
Rails 3.0
step1_tracker_planner_index GET /planner/step1(.:format)
add_tracker_planner_index GET /planner/add(.:format)
unsubscribe_tracker_planner_index GET /planner/unsubscribe(.:format)
POST /planner/unsubscribe(.:format)
Any ideas as to why this _index is being added would be much appreciated.
It is because your resource is named :planner instead of :planners that Rails decided to add the _index to any collection nested underneath. My guess it is there for readability.
The action named in the collection normally translates to a verb, so I can see why this makes sense. Take the typical photos resource example given in the routing docs:
resources :photos do
collection do
get 'search'
end
end
search_photos GET /photos/search(.:format)
But if instead we called the resources 'photo'...
resources :photo do
collection do
get 'search'
end
end
search_photo_index GET /photo/search(.:format)
In the first case, you search the "photos", and in the second case you search the "photo index".
You should be using either resource :planner either resources :planners depending on what you need. To learn about singular resource and it differences check out Rails Guides.
Following on from Semyon Perepelitsa's response, note that resource :planner expects the controller's name is PlannersController, whereas resources :planners expects PlannerController.
If you don't want to rename your controller when changing from resources to resource, you can override the default by specifying resource :planner, controller: :planner.

Rails Routes Question

I have a really simple app I've built using RoR but I'm stuck modifying my routes.
It's basically a site which lists user information - I need to change the url from:
mydomain.com/users/user-1
to
mydomain.com/user-1
Update..
I've managed to route the above request using:
match "/:id", :controller=>"users", :action=>"show"
But what I really need to do is change the route for all requests to /users/# to /
Although my route is working, all my links to show a user still point to:
/users/user-#
--- Update ---
The routing for /user-id is now working perfectly however, I'm struggling with the rest of the routing now.
I can now navigate to http://localhost/user-1
However, I basically need to remove the /user/ part completely. When I'm editing / updating a page, I end up with it going to:
/users/user-1/edit
All works fine but it then redirects to"
/users/user-1/
I really need both of these to redirect to
http://localhost/user-1/edit
Thanks
Bob
You want:
resources :users, :path => '/'
I believe get ":id" => "users#show" will be much the same except you only allow HTTP GET. Hope this works.
At the bottom of your routes
match "/:id", :to => "users#show"
There is some side effects so be ready
to rewrite your routes you should specify its name:
match "/:id", :to => "users#show", :as => :user
or, as #Whirlwin pointed, better to use just GET request as default
get "/:id", :to => "users#show", :as => :user
So now you can call:
user_path(#user)

Resources