Named route from resources takes me to show page instead of delete page - ruby-on-rails-3

I am using resources :users in routes.rb. This provides the following paths as rake routes unveils.
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
Further, I comment out the legacy wild controller route.
#match ':controller(/:action(/:id(.:format)))'
To add a delete link on my users page I add the following.
<%= link_to "Delete user", user, :method => :delete, :confirm => "Are you sure?" %>
This generated the following html code.
Delete user
I click the link and it takes me to the show page?! What's wrong?

You need to include the default Javascript files for that to work properly:
<%= javascript_include_tag :defaults %>

Related

link_to paths not linking after adding bootstrap

I have a rails application that is built on the SAAS stripe application that is hosted here:
http://railsapps.github.com/rails-recurly-subscription-saas/
I added twitter bootstrap to this and my paths are no longer creating links.
For instance my route file has:
resources :users
however no users_path is creating a link to the location in my erb files
<%= link_to 'Admin', users_path %>
Any ideas or additional information i could provide?
Rake routes:
E:\Sites>rake routes
stripe_event /stripe StripeEvent::Engine
content_gold GET /content/gold(.:format) content#gold
content_silver GET /content/silver(.:format) content#silver
content_platinum GET /content/platinum(.:format) content#platinum
root / home#index
root / home#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
update_plan PUT /update_plan(.:format) registrations#update_plan
update_card PUT /update_card(.:format) registrations#update_card
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
So this seems to be caused by bad references to images that occured when i added an asset/images directory from the wrapbootstrap template.
I added this line to my application.rb file below the config.assets.enabled=true
config.assets.paths << Rails.root.join("app", "images")
To get my images to show up after this I had to change them to be in rails format, so background: img(..) became
background: image-url('images/social_icons.png')

BEGINNER: simple link_to / routing

I'm really starting to cry here ;) Trying to make a link in a Models index view.
I have 2 simple Models: Users and Posts. There both generated with scaffolding and have working joins. A user has_many :posts and a post belongs_to :user.
What I'm trying to do in the views/post/index.html.er file is a list of the Post title and the user who it belongs to. It works well (also learning html5):
<% #posts.each do |post| %>
<p><%= link_to post.user.name, users_path %>: <b><%= post.title %></b></p>
<% end %>
Well, it works but the "users_path" is not what I want. I want to link to the specific User which the post belongs_to. I'm sorry to say that I don't get much help from http://guides.rubyonrails.org/routing.html.
How should I do this? Do I have to specify a #user in the posts_controller index-action? I reallt appreciate long and detaild answears here.
Tnk soooo much for patience with a beginner ;)
you probably have this in your routes -
resources :posts do
resources :users
end
rake routes would generate the following mapping -
post_users GET /posts/:post_id/users(.:format) {:action=>"index", :controller=>"users"}
POST /posts/:post_id/users(.:format) {:action=>"create", :controller=>"users"}
new_post_user GET /posts/:post_id/users/new(.:format) {:action=>"new", :controller=>"users"}
edit_post_user GET /posts/:post_id/users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
post_user GET /posts/:post_id/users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /posts/:post_id/users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /posts/:post_id/users/:id(.:format) {:action=>"destroy", :controller=>"users"}
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
POST /posts(.:format) {:action=>"create", :controller=>"posts"}
new_post GET /posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
The above explains the url you can use and what objects need to be passed.
For linking the user of the post -
<%= link_to "Post User details", post_user_path(post, post.user) %>
OR
<%= link_to "Post User details", url_for([post, post.user]) %>

Rails RoutingError: No route matches {:controller=>"sessions", :action=>"destroy"}

Yet when run rake:routes it appears to be there:
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
sessions POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
root /(.:format) {:controller=>"pages", :action=>"home"}
Here are the routes from routes.rb:
resources :users
resources :sessions, :only => [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/contact', to: 'pages#contact'
match '/about', to: 'pages#about'
match '/help', to: 'pages#help'
It's possible that you are not passing the :id param in your route, which is why the route is not matched, since :id is required:
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
Note: The parentheses around the :format param mean that it is optional.
I got the same error as well.But the reason is the little mistake that in the view page I write
<%= form_for(:session,url:session_path) do |f| %>
which I less the last 's' of 'sessions'.
This looks like an error I was running into when running through http://ruby.railstutorial.org/, and it turned out that I had left a few things out of routes.rb. The addition of the resource route is accompanied by the following two additional routes:
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
It's hard to see because the first route in that group is already there, so I had just glossed over the group (several times) as already being there.
resources controller adds map to method
{:action=>"method", :controller=>"controller"}
when in your case the rails seemed to ask for explicitly the map as
{:controller=>“controller”, :action=>“method”}
The :controller is before :action
This also answered Noach's question why match '/signout', :to => 'sessions#destroy' has to exist, if you rake:routes you will see it added
{:controller=>“sessions”, :action=>“destroy”} while there is already a
{:action=>“destroy”, :controller=>“sessions”} added by resources sessions

devise wrongly routes site controller

I have a problem with devise 1.1.5 on rails 3.0.3.
I have a controller "site" with an action "home". When I do a "rake routes" everything is as it should be, but when I click in my site on the sign_up link, it returns an error:
No route matches {:action=>"home", :controller=>"devise/site"}
This is correct the controller should be "site", not "devise/site".
This is in my routes.rb:
resources :articles
get "site/home"
get "site/about"
devise_for :users
But when I look with "rake routes" everything looks fine. Any ideas? Thanks!
articles GET /articles(.:format) {:action=>"index", :controller=>"articles"}
POST /articles(.:format) {:action=>"create", :controller=>"articles"}
new_article GET /articles/new(.:format) {:action=>"new", :controller=>"articles"}
edit_article GET /articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
article GET /articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /articles/:id(.:format) {:action=>"update", :controller=>"articles"}
DELETE /articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
site_home GET /site/home(.:format) {:action=>"home", :controller=>"site"}
site_about GET /site/about(.:format) {:action=>"about", :controller=>"site"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
root /(.:format) {:action=>"home", :controller=>"site"}
What is the code for your sign up link?
Try changing your routes.rb to (I think the way 'home' and 'about' are currently describe might be causing the error):
devise_for :users
resources :sites do
get :home, :on => :member
get :about, :on => :member
end
resources :articles
also remember to set your root path so that when user does get signed-in they are directed to correct view.

what's the path to destroy registration with devise

I'm trying to add a link so the user can destroy his/her own account. I'm using the built-in registration class.
In my view I have <%= link_to 'Destroy', current_user, :confirm => 'Are you sure you want to destroy your account?', :method => :delete %> pointing to localhost:3000/users/4 by example
First of all, is that the correct link to use?
Secondly, how to redirect to root path because presently it looks like it tries to redirect to user with id 4 (and it fails because it is protected).
Rake routes gives DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
Thanks in advance.
Try
<%= link_to 'Destroy', user_registration_path, :confirm => 'Are you sure you want to destroy your account?', :method => :delete %>
It's because of devise treat registration as Singular Resource.
Besides, run rake routes and you can see details about registration routing:
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
It means:
user_registration_path is a helper method that returns /users(.format)
Perform DELETE request on /users(.format) will delete the registration