In Rails 5 I've figured out how to
Overwrite the route parameter from id to something like name
Add another route for a resource
So that my routes.rb looks something like this
Rails.application.routes.draw do
resources :cats, param: :name
resources :cats do
get :preview, on: :member
end
end
I've noticed however that my additional preview route does not keep the overwritten named route parameter. Instead, when looking at the output from rake routes, I have something that looks like this.
GET /cats/:id/preview(.:format)
when what I was expecting, and trying to achieve, was a route that looks like
GET /cats/:name/preview(.:format)
How do I both add an additional route to a resource while overwriting the parameter?
You're duplicating your routes entries for cats, and you've provided the block for declaring the preview route on the entry missing the param name override. You need to provide the override and the block in the same route declaration.
Rails.application.routes.draw do
resources :cats, param: :name do
get :preview, on: :member
end
end
This gives you the route you want:
$ rake routes
Prefix Verb URI Pattern Controller#Action
preview_cat GET /cats/:name/preview(.:format) cats#preview
Related
I want to set a url as encoded url to REST API. The url I want to my route to look like this
Required: https://localhost:3000/api/v1/articles?url=https%3A%2F%2Frepository.dri.ie%2Fcatalog%2F5999pn33w&format=json
In the routes.rb I tried to set the route like this:
namespace 'api' do
namespace 'v1' do
resources :articles
get 'articles/*url' => 'articles#show'
end
end
so my route look like this
http://localhost:3000/api/v1/articles/https://repository.dri.ie/catalog/5999pn33w
how can I make the url passed as encoded url?
You have missidentified the issue here as it has nothing to do with URL encoding. Rails doesn't care about the contents of the query string when matching routes. It just matches the request by path and method.
Thus a request for https://localhost:3000/api/v1/articles?url=https%3A%2F%2Frepository.dri.ie%2Fcatalog%2F5999pn33w&format=json will alway match the index route defined by resources :articles. Remember also that routes have priority in the order they are defined.
What you need to do instead is create a route that matches /articles with a constraint:
Rails.application.routes.draw do
# Custom route to match /articles?url=xxx
get 'articles',
to: 'articles#show',
constraints: ->(request){ request.query_parameters["url"].present? }
# this must come after the custom route
resources :articles
end
I'd like to create a route that's like this:
https://example.com/appusers/1/check_for_updates.
It should basically be a resourced route so that I can do #appuser = Appuser.find(params[:id])
But I can't figure out how to list that in my routes file.
I've tried get 'appusers/:id/check_for_updates' but that throws an error.
You can do it using :member
resources :appusers do
get :check_for_updates, on: :member
end
Rails documentation
I have an AppleController
it has a def sliceme method
when I go to: /apple#sliceme
it routes to #index
In my routes.config I have
resources :apples
Why?? And what is the correct route??
Resources will create the CRUD method routes (see here)
If you want to specificity another route you can specify it like so in your routes file:
get "apple/sliceme", to: "apple#sliceme"
Or
resources :apple do
get :sliceme, on: :collection
end
To check what routes actually exist, run rake routes in the terminal
I would like to create a resourceful route on a resource member, but I can't seem to find the syntax to create the named route that I want.
namespace :admin
resources :foobars do
get :attribute, on: :member, as: :attribute
end
end
This will provide a route method called:
attribute_admin_foobar_path
I would like it to say:
admin_foobar_attribute_path
The only other way I can think of would be to reject the resources block and create a single route:
namespace :admin
resources :foobars
get 'foobars/:id/attribute', as: :foobar_attribute
end
However, I don't like this approach because it forces me to duplicate the routing structure of already existing routes...not very DRY.
Is there a way that I can create the route name that I want while still using the resources routing block?
If you do it like this:
namespace :admin do
resources :foobars do
get :attribute
end
end
You will get:
admin_foobar_attribute GET /admin/foobars/:foobar_id/attribute(.:format) admin/foobars#attribute
That is admin_foobar_attribute_path.
I have created a route in the routes.rb file like this:
match ':controller/:action/:id'
I tried invoking add_posts_path() and add_post_path() from my view and in both cases I got similar error messages like this one:
undefined method `add_post_path' for ...
I have tried declaring my match route both before and after the resources :posts declaration.
Are any route helpers created for such a route? I am unsure what helper methods can be used with such a match rule.
You can name routes with :as parameter
match '/foo/bar', to: 'foo#bar', as: 'foo_bar'
and then use foo_bar_path in your view
http://guides.rubyonrails.org/routing.html#naming-routes
If you have resources :posts, you have a helper new_post_path to add new posts. Run rake routes to see your apps routes.
add_post_path does't follow Rails routes convention for resources and if you need it, must add a custom method:
resources :posts do
get :add, :on => :collection
end
You can read more about this in this Rails guide.
When you define match ':controller/:action/:id', you set the format of your app's urls and their params, but this do not magically will define routes helpers.