Rails change path of resource - ruby-on-rails

I have this routes.rb:
Rails.application.routes.draw do
resources :students do
collection do
get :posters
get :stories
end
end
root "department#index"
get "department/about"
end
Initially, students is a database where I store students information. So for each student the URL should be students/:id. Now I want to move it to students/stories/:id.
How can I do it? Thank you!

You basically need to define a students namespace and a stories resource inside it like below:
Rails.application.routes.draw do
namespace :students do
resources :stories, only: %i[index show update destroy create] do
end
end
end
I have added all the default actions in the only list. You can remove the routes you don't need.

Related

How to custom url without change resources?

Our website is rails 4.2.8 and ruby 2.4.1, we have products to sell, so our routes like:
resources :products
resources :orders, only: [:new, :create, :destroy] do
collection do
get :success
get :fail
get :details
end
so orders url like: https://example.com/orders/new?params...
but we want add product name on url for tracking purpose, something like:
https://example.com/product_name/orders/new?params...
can I accomplish this without change route resources or controller position?
In short, No you cannot..
You have to define such nested routes in your routes.rb which will change the paths and so all dependencies needs to be changed..
To be honest,
https://example.com/product_name/orders/new?params this url doesn't make sense to me..
Ideally, correct me if I am wrong, your order must be having has_many relation with the products. However, the url conveys different story...
You have to change the routes like:
resources :products do
resources :orders, only: [:new, :create, :destroy] do
collection do
get :success
get :fail
get :details
end
end
Then you will get the route
https://example.com/product_name/orders/new?params...

Add rails routing concerns before the resource

I'm wanting to make a product resource :locationable (meaning, can be filtered by its location).
# routes.rb
concern :locationable do
member do
get 'location/:location_id'
end
end
resources :products, concerns: :locationable, action: :index
The routes above create the following route:
/products/location/:location_id
However, I'm wanting it to put the location first in the route. For example:
/location/:location_id/products
I'm wanting to use concerns for this--not nested resources.
What about changing it to
# routes.rb
concern :locationable do
resources :products, only: :index
member do
get 'location/:location_id'
end
end
Check out routing concerns from here https://gist.github.com/dideler/10020345.

Nested Routing - current_user in URL

I have to models User and Blog. Their respective urls are:
test.com/u/user_name # User
test.com/blogs # Blog
I'm trying to achieve that the blogs are nested to the user which created it. E.g. test.com/u/user_name/blogs and for an article test.com/u/user_name/blogs/article_name.
But the index for all blogs should respond to: test.com/blogs.
right now my routes are like this:
resources :users do
resources :blogs, except: [:index]
end
resources :blogs, only: [:index]
Which doesn't work.. What am i missing?
Routes
#config/routes.rb
resources :blogs, only: :index #-> domain.com/blogs -> blogs#index
resources :users, path: "u" do #-> domain.com/u/:id
resources :blogs #-> domain.com/u/:user_id/blogs/:id
end
You'll want to look up about the path argument for your routes
As a rule of thumb, you'll want to include any "non conventional" paths at the bottom of the routes file. As the routes are determined from the top to bottom, if you're capturing obscure paths near the top of the file, you'll end up causing conflict with your other routes

Adding a Route to the base URL of plural Resources

In Ruby on Rails, is there a way to add another RESTful action to the base URL of a plural resource? I'm looking for something like this:
resources :groups do
resources :users do
put on: :base, to: 'users#update_all'
end
end
Which would generate the route: [PUT] groups/:group_id/users => users#update_all
I've already tried doing this:
resources :groups do
resources :users
put 'users', on: :member, to: 'users#update_all'
end
But that doesn't preserve the value of params[:group_id] in the controller.
resources :users do
collection do
put '' => 'users#update_all' ## PUT /users
end
end
UPDATE
It would be recommended to do this though:
resources :users do
collection do
put 'update_all' ## PUT /users/update_all
end
end
Both route to the update_all action of the users controller.
RESOURCES
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Custom Nested Routes

I am trying to get custom routes based on the username rather than the ID. I have it working to get to the show page of the user but I am also trying to nest the resources so that I can see his posts and comments using the same syntax.
Example:
Works... "mysite.com/users/username/"
Does not work... "mysite.com/users/username/posts/"
routes.rb
...
# Users with the Username...
match 'users/:username' => "users#show" do
get :posts
get :comments
end
# Users with the ID...
resources :users do
get :posts
get :comments
end
...
Perhaps you can use the to_param method and update your nested routes/resources:
routes.rb:
resources :users do
resources :posts
resources :comments
end
user.rb
class User < ActiveRecord::Base
def to_param
username
end
end
..for finds in the UserController:
#user = User.find_by_username(params[:id])
(or any variation of finding by the username criteria)

Resources