Remove the Id from the route definition - ruby-on-rails

this is my routes:
PUT /welcome/:id(.:format) welcome#update
I want remove the :id from the route definition.
in other words, I want to use the update function without sending an id.
so in my routes.rb, I tried to define:
resources :welcome
match '/welcome/:id' => 'welcome#update', :via => :put
then I ran rake routes, but nothing has happened.

Do you really want to use a put request? I think you should use the a custom action to handle this. But in case you want to override the default routing, you can do it this way
match 'welcome/' => 'welcome#update', :via => 'put'
resources :welcome, :except => [:update]

use put '/welcome' => 'welcome#update'before resources :welcome so it takes precedence
UPDATE: formatting
put '/welcome' => 'welcome#update'
resources :welcome

Related

Rails route to get a resource using query string

I would like to have a custom route querystring based, to access a specified resource. For example:
/opportunities/rent/san-miguel-de-tucuman?id=45045
That route should map to the action OpportunitiesController#show, with the resource id 45045.
Thanks in advance!!!
Updated
This are my current routes:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id', to: "opportunities#show"
get 'oportunidades/alquiler/san-miguel-de-tucuman', to: "opportunities#index"
So, if I navigate to the /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 route, it go to the Opportunities#index action.
P/S: sorry, I forget to mention that I have a similar route for the index action.
Make your custom routes as:
resources: opportunities, except: :show
get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'
and pass your 'id' as opportunities_show_path(id)
EDIT
Change your routes as:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' => "opportunities#show", :as => 'opportunities_show'
get 'oportunidades/alquiler/san-miguel-de-tucuman' => "opportunities#index", :as => "opportunities_index"
Now when you want to access your show page just use opportunities_show_path(:id =>123456 )
And for index page use opportunities_index_path
Use this
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get
and pass a object to the path so created. Eg:-
something_path(#object), here #object is object that with id which will be passed in routes
Option 1
Query string parameter
// /opportunities/rent/san-miguel-de-tucuman?id=45045
match '/opportunities/rent/san-miguel-de-tucuman', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
Option 2
Add id like new parameter. More friendly.
// /opportunities/rent/san-miguel-de-tucuman/45045
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
In both cases, you need to call the route like this:
show_opportunity_url(:id => 45045)
In your controller you will get the id in params[:id]

Abstracting rails route

I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get

RESTful route overriden by named route

When defining a resources in routes.rb in Rails, I have the following problem: My resource supports the standard CRUD operations and has a custom function/route, which allows filtering. Now this custom routes matches the edit route and jumps in before the actual RESTful route.
Is there a way to prioritize the RESTful routes so that they match first?
resources :items do
get ':category(/:level)', :action => :filter, :on => :collection, :as => 'filter'
end
You should just set a simple get route ( if it is a GET request )
get 'filter', :to => "items#filter"
If you have any problems there are always RoR Guides :)
http://guides.rubyonrails.org/routing.html

Customizing User Show Route in Rails

I want, instead of /user/:id I want the default user route to be /user:created_at I was able to get /user:id to work (without the second /) however when I try to do :created_at I get an error.
Does anyone know how to fix this? Also, even though I have match 'users:id', :to => 'users#show', :as => :user, :via => :get /user/1 still is a valid link since I have resources :users in my config/routes.rb. Is there a way to remove the default /user/:id when rails compiles the resources :users?
If I understand it correctly you can either use to_param: http://apidock.com/rails/ActiveRecord/Base/to_param
so if you used in your user model
def to_param
"#{first_name}_#{last_name}"
end
then user_path(#user) would generate /users/planet_pluto for example
to prevent a route to be generated by map.resources simply use :except
map.resources :user, :except => [:show]
hope that helps

Format constraint in wildcard route

I have a CMS style application where the user can set a custom url and it routes to our "content_pages" controller.
To support this we have 3 wildcard routes defined.
I'm trying to constrain these wildcards so that they only respond to requests where the format is html, json, or xml and nothing else. This stems out of a problem where a missing favicon.ico results in a series of queries and web requests because it routes to the content_pages controller and then 404s.
Here's what I have so far but the constraint simply doesn't work. (favicon still routes)
get "/:id/edit", to: "content_pages#edit", :constraints => {:id => /.*/, :format => "[html|xml|json]"}, as: :edit_content_page
put "/:id", to: "content_pages#update", :constraints => {:id => /.*/, :format => "[html|xml|json]"}, as: :content_page
get "/:id", to: "content_pages#show", :constraints => {:id => /.*/, :format => "[html|xml|json]"}, as: :content_page
I also tried putting this into a custom constraint class but then the actions on content_pages that arent included here (like /content_pages which routes to index) don't render.
Here's the earlier resource statement that wires up the other actions.
resources :content_pages, except: [:get, :edit, :update] do
collection do
get :get_url
end
end
Any thoughts on how i can make this constraint apply without breaking our other, non-constrained actions?
If the only file type you want to exclude is .ico, then you could update your :id constraint to explicitly exclude it:
get "/:id", to: "content_pages#show", :constraints => {:id => /.+?(?<!ico)/, :format => /(html|xml|json)/}, as: :content_page
The simplest solution to this is to put a blank favicon.ico in your public directory.
This has the side benefit of allowing you to tidy up the routes:
get "/:id/edit", to: "content_pages#edit", as: :edit_content_page
put "/:id", to: "content_pages#update", as: :content_page
get "/:id", to: "content_pages#show", as: :content_page

Resources