I don't understand the difference. I've been following Michael Hartl's Rails tutorial and in my routes file I have the following:
resources :sessions, only: [:new, :create, :destroy]
match '/signin', to: "sessions#new"
match '/signout', to: "sessions#destroy", via: :delete
the site works with and without the via: :delete
The via option constraints the route to that HTTP verb. In this case, probably, the request is being made using the DELETE verb, so constraining this or not doesn't make any difference.
Note that destroy is the name of the action, delete is referencing the HTTP verb.
Related
I did
rails generate controller home index
But it adds this line to my routes.rb
get "home/index"
I thought Rails could deduce controller/method from the URL automatically? Why do I need to specify every get/post page?
Here's my complete routes.rb file:
Callisto2::Application.routes.draw do
root :to => "home#index"
resources :assets
end
root "/" works fine. so does /assets/*.
What's the problem with /home/index? I get the error:
Routing Error
No route matches [GET] "/home/index"
Try running rake routes for more information on available routes.
rake routes (run as apache user) gives me the following output:
root / home#index
Thanks for any clarifications. Not sure what I'm missing.
Edit: I didn't make this clear: I manually removed get /home/index from routes.rb to keep that file clean.
Rails used to add the so called catch all route at the bottom of your routes file:
match ':controller(/:action(/:id(.:format)))'
There was nothing 'automatic' or magical about these urls, just that every rails app started out with this route in their routes.rb
This has fallen out of favour, at least partially because it makes everything accessible over get, whereas
resources :books
Adds each route with the appropriate http verb. Listing routes explicitly is also a lot less verbose than when rails started out.
If your controller is home and the action is index your path is just /home.
You can find more information here.
In my Rails 3.1 application i have a UsersController, which gives me /users URL.
But i need /u instead for all REST actions.
What is the best practice for that?
You need to define the "path", in your case, modify routes.rb to
resources :users, :path => 'u'
My routes
devise_for :users
devise_for :admin_users, ActiveAdmin::Devise.config #I have also tried removing this for any conflicts
resources :users
The sign out link. Routes to /users/sign_out just fine
<%= link_to "Logout", destroy_user_session_path, :method => :delete %>
Trying to sign out, gives me the error:
Couldn't find User with id=sign_out
If I then remove the resource :users, I get:
The action 'sign_out' could not be found for UsersController
What's wrong? The exact same code worked with Rails 2.3.8 and the corresponding Devise version
Logging in etc. works fine.
My setup is:
Ruby 1.9.2
Rails 3.1.1.rc3
Devise 1.4.8
First of all, using the same path for UsersController and Devise isn't a great idea. I would suggest using a path like '/accounts' for Devise.
But this probably isn't the cause of your sign out problem, as devise_for :users comes before resources :users in routes.rb. What seems to be the cause is, unless there'a typo in the question, that there's no comma after destroy_user_session_path. :method => :delete will be interpreted as a parameter to destroy_user_session_path unless there's a comma.
Also, make sure you're including jquery and jquery_ujs in application.js, as these are required for :method => :delete to work.
I had an existing User model before I installed Devise so I followed the instructions here.
I even generated the devise views. But when I am at my localhost and type in localhost:3000/users/sign_in or any of the other routes available to me via devise, it doesn't work.
Also, my existing RESTful routes for users that I got from using
resources :users
are no longer available unless I have both:
devise_for :users
resources :users
but I thought I was supposed to delete the resources :users when I had the devise_for :users line in my routes file.
What is going on? Does anyone have any idea?
EDIT
The issue is when I go to users/password, it tells me that there is no user with ID=password, but this route is available to me.
I also have issues with the users/sign_in and users/sign_out. It redirects back to my root path for some reason. Sometimes it works, other times it doesn't and I am not sure why. The log looks like this:
Started GET "/users/sign_in" for 127.0.0.1 at 2011-09-16 19:05:43 -0400
Processing by Devise::SessionsController#new as HTML
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Redirected to http://localhost:3000/
What exactly doesn't work here? Are you getting anything useful in the logs or elsewhere? Also, have you setup your :users model to include the devise :recoverable, :authenticateable, ..., etc.?
You need both devise_for and resources defined in your config/routes.rb. devise_for sets up routes that will go to the devise controllers, but does not handle anything else. So you will still need normal resourceful routes for the user model to do add, update, delete, etc.
I am using Ruby 1.8.7 and Rails 3.0.3.
When I upload a file I get following error:
ActionController::InvalidAuthenticityToken
I tried adding followings to my model file:
protect_from_forgery :only => [:create, :update, :destroy]
skip_before_filter :verify_authenticity_token
How to solve it?
Check the HTML in your form and ensure that there's an element like <input name="authenticity_token" type="hidden" value="some_long_random_string" />.
If you're not using rails' form helpers or you're bypassing them with javascript somehow, you're not going to get that token in the request. That leaves you to choose between disabling the forgery protection or fixing your forms.
I just had this problem and fixed it by ensuring that <%= csrf_meta_tag %> is included wherever an html head section is defined.
This problem arose for me when I started using custom layouts and accidentally forgot to include that token.
If you define the html head section in the view itself, the csrf meta tag needs to be included there to.