I have write down in routes
match '/signup', to: 'users#new', via: [:get, :post]
when i submit the form show me error
My form code is like this
<%= form_for #user, url: {action: "new"} do |f| %>
And form submit on this url not on singup
http://localhost:3000/users/new
and show me error
No route matches [POST] "/users/new"
Rails.root: /home/jaskaran/rails_project
when i check rake output, it's show me
jaskaran#jaskaran-Vostro-1550:~/rails_project$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
microposts GET /microposts(.:format) microposts#index
POST /microposts(.:format) microposts#create
new_micropost GET /microposts/new(.:format) microposts#new
edit_micropost GET /microposts/:id/edit(.:format) microposts#edit
micropost GET /microposts/:id(.:format) microposts#show
PATCH /microposts/:id(.:format) microposts#update
PUT /microposts/:id(.:format) microposts#update
DELETE /microposts/:id(.:format) microposts#destroy
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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
signup POST|GET|POST /signup(.:format) users#new
posts_new GET /posts/new(.:format) posts#new
posts_create POST /posts/create(.:format) posts#create
I am learning ruby and rails 4 +
Even though your route maps to the new action of the users controller, your form needs to point to the URI pattern, in your case /signup.
When in doubt, you can find the URI pattern and other information in your routes. Run rake routes in your console and you'll see this:
signup GET|POST /signup(.:format) users#new
Rails generates helper methods to use in your views for each of the routes, based on the route prefix. In your case you can use the helper method signup_path. To fix the problem, change your view code to this:
<%= form_for #user, url: signup_path do |f| %>
Check the Rails documentation for more information.
I get this error:
Failures:
1) UsersController DELETE 'destroy' should sign a user out
Failure/Error: delete :destroy
ActionController::UrlGenerationError:
No route matches {:controller=>"users", :action=>"destroy"}
My test is:
it "should sign a user out" do
test_sign_in(Factory(:user))
delete :destroy
expect(controller).to_not be_signed_in
expect(response).to redirect_to(root_path)
end
The test_sign_in function is in the spec helper:
def test_sign_in(user)
controller.sign_in(user)
end
My rake routes:
Prefix Verb URI Pattern Controller#Action
sessions_new GET /sessions/new(.:format) sessions#new
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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root GET / pages#home
contact GET /contact(.:format) pages#contact
about GET /about(.:format) pages#about
help GET /help(.:format) pages#help
signup GET /signup(.:format) users#new
signin GET /signin(.:format) sessions#new
signout GET /signout(.:format) sessions#destroy
pages_home GET /pages/home(.:format) pages#home
Anyone knows how can I solve this error?
Your route is defined as
DELETE /users/:id(.:format) users#destroy
which means that the route is expecting something like
DELETE /users/4
Looking at your test, you are just requesting DELETE /users, this was derived from this error message:
ActionController::UrlGenerationError: No route matches {:controller=>"users", :action=>"destroy"})
So, you need to modify your test to handle the :id part of the route. This is un-tested, but you're roughly looking for:
user = Factory(:user)
test_sign_in(user)
delete :destroy, id: user.id
ooooh god!! I found the error. I accidentally was writing the code in the users_controller_spec and I should do this in the sessions_controller_spec.
Thanks Andreas for trying to help!
Lets say I have an UsersController that contains an action #new. In my routes file I map with the following:
match 'signup', to: 'users#new'
This action can now be accessed by both /signup and /users/new. How do I restrict it to only the custom route.
I apologize if this has been answered, but am new to this. I've searched, but haven't found the answer. Possibly due to my not knowing how to concisely phrase this.
You can exempt the new route from the users resource, and replace it with your custom route:
resources :users, except: [:new]
get 'signup', to: 'users#new', as: "new_user"
Resulting in:
users GET /users(.:format) users#index
POST /users(.:format) users#create
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
new_user GET /signup(.:format) users#new
This is a design problem that I am trying to figure out. I will explain what I have right now, and what I would like to have:
1. Actual design
I have a defined a resources :users and by doing so I have defined different actions such as new, create and update in the Users controller. This is working as expected by following urls like users/new , users/:id, etc...
Now I want to go one step forward, and I want to be able to do the following...
2. What am I looking for
I want to be able to have a route like this:
users/overview/profile - This should be equivalent to `users/:id` (show action)
users/overview/network - This should be equivalent to users/:id/network (list of networks for that user)
3. My idea
My first idea was to define something like this:
resource :users do
namespace :overview do
resource :networks
end
end
But this would work for urls like: users/:id/overview/networks and I don't want the user id to be shown in the URL. So my questions are:
1 - How can I deal with users/overview/networks instead of users/:id/overview/networks , assuming that I can get the user id from session.
2 - How can I be able to manage URLs like this: users/overview/profile where actually a profile is just the show method of users/:id Right now I have defined all the actions in the users controller and everything is working fine (new,delete,create,update...) I just don't know how to move into that "namespace" overview/profile
I have tried same thing you tried, and it is returning your desired results only, not sure what is your problem. Posting rake routes result here.
users_overview_networks POST /users/overview/networks(.:format) {:action=>"create", :controller=>"overview/networks"}
new_users_overview_networks GET /users/overview/networks/new(.:format) {:action=>"new", :controller=>"overview/networks"}
edit_users_overview_networks GET /users/overview/networks/edit(.:format) {:action=>"edit", :controller=>"overview/networks"}
GET /users/overview/networks(.:format) {:action=>"show", :controller=>"overview/networks"}
PUT /users/overview/networks(.:format) {:action=>"update", :controller=>"overview/networks"}
DELETE /users/overview/networks(.:format) {:action=>"destroy", :controller=>"overview/networks"}
users POST /users(.:format) {:action=>"create", :controller=>"users"}
new_users GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_users GET /users/edit(.:format) {:action=>"edit", :controller=>"users"}
GET /users(.:format) {:action=>"show", :controller=>"users"}
PUT /users(.:format) {:action=>"update", :controller=>"users"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"users"}
scope :path => 'users/overview' do
match ':id/profile', :to => 'users#show'
match ':id/network', :to => 'users#network'
end
In my users photo album page they see photos they have uploaded and each photo has a 'make default' link on it.
When the user clicks make default, then the id of the photo is stored in the photo_id column of my profiles table.
The issue is redirecting them back to:
localhost:3000/settings/photo_gallery/:id
Is there a way I can redirect back to the photo album using the id of the photo that has just been set as the default? Can Rails find what album I want to redirect to just by looking at the id of a photo, as a photo belongs to a photo album, and a photo album has many photos?
I have the following tables in my database:
User(s): has one profile, has many PhotoAlbums
Profile(s): belongs to user
PhotoAlbum(s): belongs to user, has many photos
Photo(s): belongs to PhotoAlbum
Controller action:
def set_default_profile_photo
photo = Profile.find_by_user_id(current_user.id)
photo.photo_id = params[:photo_id]
photo.save
redirect_to "**HERE IS WHERE I'D LIKE TO REDIRECT TO THE PHOTOALBUM THE PHOTO IS IN**"
flash[:success] = "Default photo set!"
end
My routes:
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 GET /sessions(.:format) {:action=>"index", :controller=>"sessions"}
POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
edit_session GET /sessions/:id/edit(.:format) {:action=>"edit", :controller=>"sessions"}
session GET /sessions/:id(.:format) {:action=>"show", :controller=>"sessions"}
PUT /sessions/:id(.:format) {:action=>"update", :controller=>"sessions"}
DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
passwords GET /passwords(.:format) {:action=>"index", :controller=>"passwords"}
POST /passwords(.:format) {:action=>"create", :controller=>"passwords"}
new_password GET /passwords/new(.:format) {:action=>"new", :controller=>"passwords"}
edit_password GET /passwords/:id/edit(.:format) {:action=>"edit", :controller=>"passwords"}
password GET /passwords/:id(.:format) {:action=>"show", :controller=>"passwords"}
PUT /passwords/:id(.:format) {:action=>"update", :controller=>"passwords"}
DELETE /passwords/:id(.:format) {:action=>"destroy", :controller=>"passwords"}
profiles GET /profiles(.:format) {:action=>"index", :controller=>"profiles"}
POST /profiles(.:format) {:action=>"create", :controller=>"profiles"}
new_profile GET /profiles/new(.:format) {:action=>"new", :controller=>"profiles"}
edit_profile GET /profiles/:id/edit(.:format) {:action=>"edit", :controller=>"profiles"}
profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
PUT /profiles/:id(.:format) {:action=>"update", :controller=>"profiles"}
DELETE /profiles/:id(.:format) {:action=>"destroy", :controller=>"profiles"}
emails GET /emails(.:format) {:action=>"index", :controller=>"emails"}
POST /emails(.:format) {:action=>"create", :controller=>"emails"}
new_email GET /emails/new(.:format) {:action=>"new", :controller=>"emails"}
edit_email GET /emails/:id/edit(.:format) {:action=>"edit", :controller=>"emails"}
email GET /emails/:id(.:format) {:action=>"show", :controller=>"emails"}
PUT /emails/:id(.:format) {:action=>"update", :controller=>"emails"}
DELETE /emails/:id(.:format) {:action=>"destroy", :controller=>"emails"}
root / {:controller=>"users", :action=>"new"}
success /success(.:format) {:action=>"success", :controller=>"users"}
login /login(.:format) {:action=>"new", :controller=>"sessions"}
logout /logout(.:format) {:action=>"destroy", :controller=>"sessions"}
reset_password /reset_password(.:format) {:action=>"new", :controller=>"passwords"}
setup_new_password /setup_new_password(.:format) {:action=>"edit", :controller=>"passwords"}
settings /settings(.:format) {:action=>"settings", :controller=>"users"}
settings_account /settings/account(.:format) {:controller=>"users", :action=>"account"}
settings_edit_profile /settings/edit_profile(.:format) {:controller=>"profiles", :action=>"edit_profile"}
/:username(.:format) {:controller=>"users", :action=>"show"}
change_password /change_password(.:format) {:action=>"change_password", :controller=>"users"}
profile_photo_set_default /profile_photo/set_default(.:format) {:controller=>"photo_albums", :action=>"set_default_profile_photo"}
album_photo_set_default /album_photo/set_default(.:format) {:controller=>"photo_albums", :action=>"set_default_album_photo"}
photo_albums GET /settings/photo_gallery(.:format) {:action=>"index", :controller=>"photo_albums"}
POST /settings/photo_gallery(.:format) {:action=>"create", :controller=>"photo_albums"}
new_photo_album GET /settings/photo_gallery/new(.:format) {:action=>"new", :controller=>"photo_albums"}
edit_photo_album GET /settings/photo_gallery/:id/edit(.:format) {:action=>"edit", :controller=>"photo_albums"}
photo_album GET /settings/photo_gallery/:id(.:format) {:action=>"show", :controller=>"photo_albums"}
PUT /settings/photo_gallery/:id(.:format) {:action=>"update", :controller=>"photo_albums"}
DELETE /settings/photo_gallery/:id(.:format) {:action=>"destroy", :controller=>"photo_albums"}
photos GET /settings/photo_gallery/photos(.:format) {:action=>"index", :controller=>"photos"}
POST /settings/photo_gallery/photos(.:format) {:action=>"create", :controller=>"photos"}
new_photo GET /settings/photo_gallery/photos/new(.:format) {:action=>"new", :controller=>"photos"}
edit_photo GET /settings/photo_gallery/photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"}
photo GET /settings/photo_gallery/photos/:id(.:format) {:action=>"show", :controller=>"photos"}
PUT /settings/photo_gallery/photos/:id(.:format) {:action=>"update", :controller=>"photos"}
DELETE /settings/photo_gallery/photos/:id(.:format) {:action=>"destroy", :controller=>"photos"}
This is what you want:
redirect_to request.referrer
For further reference:
http://en.wikipedia.org/wiki/HTTP_referer
redirect_to :back worked for me but I want to see if this was the right choice
http://api.rubyonrails.org/files/actionpack/lib/action_controller/metal/redirecting_rb.html
In Rails 5 it was introduced the function:
redirect_back(fallback_location: root_path)
It does redirect back whenever the HTTP_REFERER is known. Otherwise it redirects to the fallback_location.
The redirect_to :back is deprecated and will be removed from Rails 5.1.
In one project we used the session for temporary storage, because redirect_to :back did not work for us.
We had an def new where we set session[:return_to] = request.referer
in the def create we added redirect_to session[:return_to].
I do not know anymore, why we could not use redirect_to :back
If you came from the photo_album page, you should be able to do:
redirect_to :back
Otherwise, you should be able to do a named route like:
redirect_to photo_album_path(photo.album_id) # or whatever the association key is
BTW, why do you have photo_albums mapping to photo_galleries? It's a lot less confusing if you named your resources and routes in a similiar manner. ie: if you wanted your route endpoints to use /photo_galleries you should name your resource PhotoGallery.