Rails 5 - how to add a prefix for a scaffold route? - ruby-on-rails

I have generated a scaffold model car.
So if I want to create a new car, the URL looks like this: /cars/new. I need to pass another information to the URL and the easiest way is to simply add it as a parameter in the end, so the URL would look something like /cars/new?listing=123.
What I am trying to do is to get something like this in the URL: /l/123/cars/new (l = listings). Same for the other actions as well (eg. /l/123/cars/4/edit.
How do I need to modify the routes for such an example? listing is another model, and every listing has a car.
Thank you

AFAIK, you can't achieve this with resourceful way. You have to define non-resourceful routes to achieve what you want. For instance, the route for cars#new would be defined like below
get /:listing_id/:extra_param/cars/new, to: "cars#new"

I think you can use nested resources:
resources :listings do
resources :cars
end
It will generate routes like listings/:listing_id/cars/:id. If you do not have listings controller or you do not want to generate routes for it just use resources :listings, only: [] do ...

Related

How to set child index path without specific parent id in Rails?

I have route.rb like this:
resources :companies do
resources :company_jobs
end
And rails routes:
company_company_jobs GET /companies/:company_id/company_jobs(.:format) company_jobs#index
I need to add a link to show all company_jobs model without specific company_id like this:
Any one how can I config route to do this? Thank you so much!
What your routing implies is that you want all company_jobs that belong to company identified by :id. Assuming that you have a model association set up between the two models, this would be referenced as <%= link_to company_company_jobs(#company) %> - rails will fill in the id for you.
If instead, you just want to include all jobs no matter which company, you could change your routes to:
resources :companies do
collection do
get :company_jobs
end
resources :company_jobs
end
This will create a new route companies_company_jobs_path company#company_jobs
For this route to work, you will need to add the following to companies_controller.rb
def company_jobs
end

Change Rails namespaced route to personalized params route

TL;DR: I want to have username621/posts/title-of-post instead of member/posts/1
The changing of post id to post title was easy enough since I used the freindly_id gem to generate the slugs.
However, I am having difficulty routing to a personalized params route instead of the current namespaced route. Here is the current routing:
namespace :member do
resources :posts
end
I want to replace the member namespace to user's username. So if their username is user123, the route should be user123/posts/title-of-post.
I think that this is not very standard Rails routing and tried looking for similar questions with no results.
for more complicated routes.rb, add a path option
namespace :member, path: ":user_id" do
resources :posts
end
should get what you want, e.g. http://localhost:3000/621/posts/1
then we just have to add friendly_id to User and Post to have it become something like http://localhost:3000/username621/posts/title-of-post
however, you'll need to pore through the codebase for things like member_post_path(post) and change to member_post_path(post.user, post)
Try removing the namespace and adding path option:
resources :posts, path: '/:username/posts/'
Then if you access /username621/posts/title-of-post in your controller you'll see params[:username] = 'username621'
If you have other paths of the form /something/posts add them above this route, otherwise they will be caught by :username.

how to change multiple nested routes in rails

i'm doing a course system, and i would like to simplify the routes.
i routed like this:
resources :courses do
resources :modules do
resources :lesson
end
end
and returned this:
/courses/:course_id/modules/:module_id/lesson/:id
/courses/:course_id/modules/:id
/courses/:id
etc...
i want my routes like that:
/courses/:course_name/:module_name/:lesson_name
/courses/:course_name/:module_name/
/courses/:course_name/
etc...
but how?! :(
in the routes file
get "/courses/:course_name/:module_name/:lesson_name", as: :courses
then you should be able to generate the path:
courses_path(course_name: course.name, module_name: module.name, lesson_name: lesson.name)
But I would recommend against it as:
1) This is fighting conventions: don't expect good support for this, none of the new developers who join the project will like you for this.
2) You will have to make sure all course/module/lesson names are unique and url-friendly
3) You'll have to make sure the names never change, because then the urls would change.
I would advice sticking to the default nested paths and overriding #to_param on every module
to smth like:
def to_param
"#{super}-#{name.downcase.gsub(' ', '-')}" # you need a better regex here
end
so the urls look like
/courses/33-computer-science/modules/23-engineering/lesson/56-design-patterns
More about custom routes: http://guides.rubyonrails.org/routing.html#dynamic-segments

Extract params fro url in rails

From the url below how can I extract the value 1?
`http://localhost:3000/category/products/1`
I tried params[:id] and params[:products][:id] but got nothing.
Did you make suitable change in your routes.rb file? You need to include something like
GET /category/products/:id , ...
to make it work with params[:id].
Routes
The direct answer to your question is to fix your routes.rb file
As per the Rails RESTful routing structure, you should be able to use a named scope to achieve this:
#config/routes.rb
scope 'category' do
resources :products
end
#/category/products/1 -> params[:id]
Nested
What I recommended above should fix your problem directly
However, I think you're trying to achieve nested resources. If this is the case, you should use something like this:
#config/routes.rb
resources :categories do
resources :products
end
This will allow you to do:
#categories/:id/products/:product_id

ruby on rails routing helper: how to create select_clients_path(client)

I have client standard resource with CRUD, but I would like to make extension with action select, so that I can have select_clients_path(client).
In clients_controller I have created action select, but I dont know how to create correct routing rule
for now I have created:
resources :clients do
get 'select'
end
but this generates /clients/select.2 but i would like somthing like /clients/select/2 or /clients/select?id=2
thank you
Dorijan
resources :clients do
collection do
get :select
end
end
will create a 'clients/select' route, to which you can pass parameters like '?client_ids=2...' and work with several client records.
alternatively,
resources :clients do
member do
get :select
end
end
will create a 'clients/:id/select' route to work with a single client record
Take a look at http://guides.rubyonrails.org/routing.html#adding-more-restful-actions for more about this functionality, but those blocks will get you pretty far.

Resources