Why is Rails generating two routes for the same resource - ruby-on-rails

Given the following routes file:
Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users
resources :users do
resources :wishlists, :only => [:create] do
post :action => :create, :on => :collection
resources :items, :only => [:create, :update, :remove_item] do
post :action => :create, :on => :collection
put :action => :update
delete :action => :remove_item
end
end
end
end
Rails generates routes including the following routes which conflict:
PUT /wishlists/:wishlist_id/items/:item_id(.:format) items#update
wishlist_item PUT /wishlists/:wishlist_id/items/:id(.:format) items#update
Why does the first of these get generated? I would expect only the second one (which includes the path helper)
I'm using Rails 4.1.4

Because your are declaring 2 times the same route:
the first one in resources :items, :only => [:create, :update, :remove_item] generates this resource: /wishlists/:wishlist_id/items/:id(.:format)
the second in put :action => :update generates this one: /wishlists/:wishlist_id/items/:item_id(.:format)
You should be using only 1 of them (I recommend the first one).
If you want more information on routing, you should definitely go on this page.

Related

Routes for create action

I have my user_controller and the action create. I do however want to set a custom route to the create action so when the user hits register the create action sets the url to user/thank-you. I have added the following match to my routes file but the url stays unchanged after hitting the register button. Here is part of my routes file until the line with thank-you.
get '/:locale' => 'pages#home'
root :to => 'pages#home'
scope "(:locale)", locale: /en|es/ do
get "javascripts/dynamic_cities"
get "javascripts/dynamic_cities_to_stadia"
get "javascripts/dynamic_stadia"
get "javascripts/dynamic_modality_to_max_players"
get "tournaments/edit_info"
get "tournaments/seasons"
get "league_rosters/cancel_new"
get "tournament_administrations/cancel_new"
resources :admin_tasks, :only => [:index]
resources :sports
resources :countries
resources :cities
resources :stadia
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :teams
resources :tournaments
resources :leagues
resources :brackets, :only => [:new, :create, :edit, :update, :show]
resources :labs, :only => [:show, :edit]
resources :seasons, :only => [:edit, :show, :destroy]
resources :matches, :only => [:show, :edit, :update]
resources :player_participations
resources :highlights
resources :users do
resources :requests, :name_prefix => "user_"
end
# match 'league/:id/subscription' => 'league_rosters#new', :as => :subscription
match '/contact', :to => 'pages#contact'
match '/terms_and_conditions', :to => 'pages#terms_and_conditions'
match '/about_us', :to => 'pages#about_us'
match '/cookie_excluder', :to => 'pages#cookie_excluder'
match '/vacancies', :to => 'pages#vacancies'
match '/signup', :to => 'users#new'
match '/thank-you', :to => 'users#create'
Here is also the create method in the users_controller.rb
def create
# Note: This function is repeated in request controller in the invitations part. So any change should be added to request controller aswell
#user = User.new(email: params[:user][:email].downcase,
name: params[:user][:name].capitalize,
password: params[:user][:password],
password_confirmation: params[:user][:password_confirmation],
language: params[:user][:language])
#for user_menu
#title = t("user.new.title")
if #user.save
confirmation_code = "#{#user.id}#{random_string}"
if #user.update_attributes(confirmation_code: confirmation_code)
UserMailer.welcome_email(#user).deliver
vars = Hash.new
vars[:cc] = "#{confirmation_code}"
confirmation_url = url_maker(params[:request_protocol], params[:request_host_with_port], params[:request_locale], "#{email_confirmation_path}", vars)
UserMailer.confirm_email(#user, confirmation_url).deliver
sign_in #user
#user_tournaments = current_user.tournaments.all(:order => "name ASC")
#user_teams = current_user.teams.all(:order => "name ASC")
else
render 'new'
end
else
render 'new'
end
end
Is it possible for this to work the way I am approaching it or I will have to make a redirect and a thank-you page, which I can easily manage through my pages controller?
Move your thank-you route above the resources block as follows:
...
get "league_rosters/cancel_new"
get "tournament_administrations/cancel_new"
# Move route here
match '/thank-you', :to => 'users#create', via: [:post]
resources :admin_tasks, :only => [:index]
resources :sports
...
I'm also adding via: [:post] to the route since:
Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.
[http://guides.rubyonrails.org/routing.html]

Rails Adding New Page to Resource (with parameter)

I am trying to add a few new pages to a rails resource that I am creating.
What I am doing in my routes file is as follows:
resources :users, :only => [:index, :show] do
collection do
get :show_subpage1
end
end
When I look at my routes, show_subpage1 shows up, but not in the format I want. What shows up in the routes is:
show_subpage1_users GET /users/show_subpage1(.:format)
When what I WANT the route to be is:
show_subpage1_users GET /users/:id/show_subpage1(.:format)
(with the ID).
How would I go about doing that in rails?
To get:
show_subpage1_users GET /users/:id/show_subpage1(.:format)
do not define :show_subpage1 as a collection route:
resources :users, :only => [:index, :show] do
get :show_subpage1
end
or you could define it as a member route as follows:
resources :users, :only => [:index, :show] do
member do
get :show_subpage1
end
end
Also, I'm unsure why you have :only => [:index, :show] defined if you are also going to have a member route :show_subpage1. I assume you do want to add add :show_subpage1 to the only array, i.e. resources :users, :only => [:index, :show, :show_subpage1] do.
Please take a read on "Adding More RESTful Actions"
there are two ways with resources member or collection
resources :users, :only => [ :index, :show ] do
# /users/:id/profile
get 'profile', :on => :member
# /users/profile
get 'profile', :on => :collection
end
hope this helps

add new routes to a resource without the added param?

I would like to add a url to my comments route so I can call "post_comments_latest_path". I added something like 'get "comments/latest" => "comments#latest", :as => "latest"' but the route adds and the :commend_id to the path that is not needed. Any suggestions?
resources :posts, :except => [:index] do
resources :comments, :except => [:index, :show] do
post "replies" => "comments#create_reply", :as => "create_reply"
get "replies/new" => "comments#new_reply", :as => "new_reply"
end
end
This should work:
resources :posts, :except => [:index] do
resources :comments, :except => [:index, :show] do
post "replies" => "comments#create_reply", :as => "create_rely"
get "replies/new" => "comments#new_reply", :as => "new_reply"
get "latest", :on => "collection"
end
end
A Member route is one that links to a specific resource; requires an id.
A Collection route is one that links to a resource collection; does not require an id.
See the Rails Routing Guide for more information.

Devise routing error: "Unknown action -- The action 'create' could not be found for UsersController"

My devise set up was working fine before, but now, for some reason, whenever I try to sign up a new user, it tries to call users#create instead of registrations#create. I think it must be a problem with my routes.rb file. I recently added a new resource, "preferences", to my application, so the routing might be wonky:
Indexer2::Application.routes.draw do
resources :preferences
get "home/index"
resources :posts
resources :users
devise_for :users, :controllers => {:registrations => 'registrations', :invitations => 'invitations'}, :except => [:show] do
get "/signup" => "devise/registrations#new", :as => 'user_signup'
get '/logout' => 'devise/sessions#destroy', :as => 'user_logout'
get '/login' => "devise/sessions#new", :as => 'user_login'
end
match '/welcome' => 'pages#welcome'
resources :preferences, :except => [:destory, :edit, :create, :new, :index, :show] do
collection do
post "make_feed_preference"
post "change_preference"
end
end
root :to => "home#index"
end
Your UsersController should have create method.
If you don't want to write your own registration logic just do inheritance from Devise::RegistrationsController < DeviseController:
controller UsersController < Devise::RegistrationsController
#....
end
This will include default Devise methods.

Rails 3 Routes: Override Topics#show with Posts#index

Pretty simple question, but I can't seem to figure out how to do this, even after scouring the Rails Routing guide.
Assume Topics has nested resource Posts.
The Posts for a Topic are all listed in Posts#index (/topics/:topic_id/messages). Topics#show does not serve any purpose. I would like Posts#index to be retrieved when the request is for /topics/:topic_id, without having to stick a redirect in the Topics controller.
Thank you!
UPDATE
I was able to get the desired result with this:
routes.rb
match 'forums/:forum_id' => 'topics#index', :as => 'forum_topics', :via => :get
match 'topics/:topic_id' => 'messages#index', :as => 'topic_messages', :via => :get
resources :forums, :shallow => true, :except => :show do
resources :topics, :shallow => true, :except => :show do
resources :messages
end
end
However, I'm not sure if this is the best method.
UPDATE 2
My method above breaks the other CRUD methods (like #create). Still looking for a solution to keep /messages out of the url.
add this route
get "/topics/:topic_id" => redirect("/topics/%{topic_id}/messages")
UPD
without redirecting:
get "/topics/:id" => "topic::Messages#index"
Or, if you're using shallow:
get "/topics/:id" => "messages#index"
resources :forums, :shallow => true, :except => :show do
resources :topics, :shallow => true, :except => :show do
resources :messages
end
end

Resources