Placing User ID's within Routes with Devise - ruby-on-rails

I currently have a default devise set up and wanting to put the user id's within my logged in routes and leave the others alone such as Welcome,FAQ, About us etc.
Such as
www.example.com/user-id/offers
My Current Routes File
devise_for :users
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :offers do
member do
put :tourcomplete
end
end
resources :categories, only: :show
root 'welcome#index'
get '/balance', to: 'balance#show', as: 'balance'
patch '/balance', to: 'balance#paypal', as: 'balance_paypal'
patch '/withdraw', to: 'balance#withdraw', as: 'balance_withdraw'
I can't find any docs on this and my previous answer to a similar question was very vague and not helpful.
Thanks

You will need to use a nested routes:
resources :users do
resources :offers do
member do
put :tourcomplete
end
end
end
so now your routes will be /users/:id/offers, run rake routes to check your routes.
if you want to exclude users then you will have to specify the route yourself:
match ':user_id/offers' => 'offers#index'

Related

Rails 5 rename CRUD route

I have the following Rails 5 resources, and I want the URL to show /welcome on huddles#index (as well as for the huddles_path url helper). e.g, I don't want huddles_welcome_path. Can this be done? Thank you!
resources :huddles do
get '/welcome', to: 'huddles#index'
resources :invitations
end
Move the route outside of the huddles resource if you don't want to include huddles/ in the route:
resources :huddles do
resources :invitations
end
get '/welcome', to: 'huddles#index'
resources :huddles do
get '/welcome', to: 'huddles#index', :as => :huddles_path
resources :invitations
end
Just Add a :as => :namethatyouwant

Rails routes.rb use get to add suffix to path

I'm trying to figure out how can i build this route in rails:
resources :cases do
resource :profile do
get 'regions', :to => "cases#regions"
end
end
This code will generate method case_regions_profile_path, but i want it upside down: case_profile_regions_path, is it possible using get 'rule'? I just want to point this path to a controller with specific action.
Its typo you have used resource instead of resources for profiles
resources :cases do
resources :profile do
get 'regions', :to => "cases#regions"
end
end
This generates:
case_profile_regions GET /cases/:case_id/profile/:profile_id/regions(.:format) cases#regions

Rails custom and default routes

I'm trying to define custom routes to my controller and I need to use some of the default routes too. Is there any simple solution?
So far I've something like this
resources :users do
member do
get 'users/:id', to: 'users#show'
delete 'users/:id', to: 'users#destroy'
end
collection do
post 'users', to: 'users#create'
post 'users/login', to: 'users#login'
end
end
resources :users, :only => [:show, :destroy, :create, :login]
I don't need nor want the index route but with this settings it's still trying to route GET users/ to user_controller index method.
I know that there is probably some simple and obvious answer but I'm not able to find it.
Thank's in advance.
You got your routes wrong. The resources :users generates seven default routes which include the index route as well. You need to tweak the code to below
resources :users, :only => [:show, :destroy, :create] do
collection do
post 'login', to: 'users#login'
end
end
Note:
If you noticed, I've removed the custom routes for show,create and delete as they are generated by default.
Your first line defines the route to the index action. Define a resource once only. Read the routing guide.
resources :users, :except => [:index] do
collection do
post 'users/login', to: 'users#login'
end
end
Run rake routes from the command line in your project root folder to see all your route definitions.

Setting up routes in rails

When I put the following code in my routes config :
resources :users do
end
I get all the CRUD operations routes. i.e
/users/new
/users/:id/edit
and so on.
How do I configure routes so I get route like this :
/users/lookup/:search_query
And when users reaches this routes he/she should be taked to lookup method of my controller
I would do :
resources :users do
get :lookup, on: :collection
end
And I would pass the search_query as a parameter. With that you will be more flexible.
resources :users do
get '/lookup/:search_query' => 'users#lookup', on: :collection
end
resources :users do
end
match '/users/lookup/:search_query' => "users#lookup", :as => :user_lookup

Routing error caused by nested routes in Rails 3 project

Here's my problem:
I have a web app, in which users can create posts.
User and post are created simultaneously - I extract the user's email from the post to create his user entry. (No password/login/registration etc required)
In my routes.rb file, I have posts nested with users (see attached)
Now, here is my question:
Where should the posts#new creation form be? Currently I have it at /posts/new but this is clearly wrong, I am getting a routing error.
Grateful for any feedback.
routes.rb
Mysalary::Application.routes.draw do
resources :users do
resources :posts
end
resources :profiles
resources :pages
get "pages/home"
get "pages/about"
get "pages/legal"
get "pages/feedback"
root :to => 'posts#new'
end
I would add posts on it own, so to have both you would have:
routes.rb
resources :users do
resources :posts
end
resources :posts
You have posts only as a nested resource, so you would find it at /users/:user_id/posts/new
If you want to reach it at /posts/new, just un-nest resources :posts. You can also leave it nested and repeat it outside the nesting, then it would be reachable both ways.
Remember to run rake routes in the console.

Resources