scope in rails routes not prepending paramter in URL - ruby-on-rails

I have a requirement that I have to append abc/admin in each url. for e.g.
http://0.0.0.0:3000/abc/admin/admins/sign_in # USING ADMIN AS DEVISE MODEL
http://0.0.0.0:3000/abs/admin/products
http://0.0.0.0:3000/abs/admin/products/:id
http://0.0.0.0:3000/abs/admin/categories
etc. Now I tried with
scope "abc/admin" do
resources :products, :categories
end
It creates some URL eg:
products GET /abc/admin/products(.:format) products#index
POST /abc/admin/products(.:format) products#create
new_role GET /abc/admin/products/new(.:format) products#new
for Admin Model I tried using
scope "abc/admin" do
devise_for :admins, :controllers => {:sessions=>"admin/sessions", :registrations=>'admin/registrations'}
end
It also create URL like:
admin_registration POST /abc/admin/admins(.:format) admin/registrations#create
new_admin_registration GET /abc/admin/admins/sign_up(.:format) admin/registrations#new
Now new_admin_registration_path redirect to http://0.0.0.0:3000/admins/sign_up It is not considering the scope. I don't know how to achieve scope parameter in URL for devise as well as for other controller.

#Choco is right, you should only declare a namespace once. Then everything goes inside that single block.
namespace "abc/admin" do
resources :products, :categories
devise_for :admins, :controllers => {:sessions=>"admin/sessions", :registrations=>'admin/registrations'}
end
Then your path helper becomes new_abc_admin_admin_registration_path. See here for info: difference between scope and namespace of ruby-on-rails 3 routing

Related

Devise scoped member routes issue

I'm using devise for sign up and ActiveStorage for image upload. For the delete/purge function to work I have this route
devise_scope :user do
scope module: :users do
resources :registrations do
member do
delete :delete_image_attachment
end
end
end
end
But another place in my routes file I have this route
devise_for :users, controllers: {:registrations => "users/registrations"
}
It makes some of my pages not working. I have read somewhere that it's because registrations are declared two times. How can I make it work?
Any help would be much appreciated
If you use resources :registrations, only: [] do... that will create the parent route that you need without overwriting any of the routes provided by devise. Allowing you to make your nested routes :D

No route matches [PUT], resource and method are defined though

I'm using postman to make a PUT or PATCH request, but it says No route matches [PUT] "/api/registrations"
my URL looks like this
http://localhost:3000/api/registrations?id=5&status=approved"
My routes.rb file:
Rails.application.routes.draw do
scope :api do
resources :professors
resources :registrations
resources :schedules
resources :notifications
resources :users
resources :meetings
resources :courses
resources :students
end
end
I have a defined update method in my RegistrationsController and My POST and GET routes work.
The URL you're using is incorrent. You should not pass id in query, but in path.
A correct URL is
http://localhost:3000/api/registrations/5?status=approved
Rails sets id as last element of a resourceful route.
Docs say:
resources :photos
(...)
PATCH/PUT /photos/:id photos#update update a specific photo
Run rails routes on the command line to get the proper URL pattern and it's matching Controller action.

How to use namespace in rails without namespace in url

I have a question about routes in rails when using namespaces.
Example code:
namespace :api do
# /api/... Api::
namespace :v1 do
devise_for :users, :controllers => {sessions: 'api/v1/users/sessions', :registrations => "api/v1/users/registrations", :password => 'api/v1/users/passwords'}
resources :events do
namespace :informations do
resources :agendas
resources :attendees
resources :polls
resources :presentatios
resources :speakers
resources :sponsors
resources :votes
resources :vote_options
end
end
end
end
I check my url in console with grep agenda, and it results in:
/api/v1/events/:event_id/informations/agendas
How can I remove namespace information from url without removing namespace from routes?
You can use the module option to add a namespace (as in a module wrapping) to the controllers:
resources :events, module: 'v1/api'
Prefix Verb URI Pattern Controller#Action
events GET /events(.:format) v1/api/events#index
POST /events(.:format) v1/api/events#create
new_event GET /events/new(.:format) v1/api/events#new
edit_event GET /events/:id/edit(.:format) v1/api/events#edit
event GET /events/:id(.:format) v1/api/events#show
PATCH /events/:id(.:format) v1/api/events#update
PUT /events/:id(.:format) v1/api/events#update
DELETE /events/:id(.:format) v1/api/events#destroy
But it makes no sense to use a versioned API controller without versioned urls! That is unless you believe in either using a custom request header or custom accept type. Both of which had some diehard REST purist followers but are now scarce due to the the fact that they are just too dang difficult to test.
http://www.troyhunt.com/2014/02/your-api-versioning-is-wrong-which-is.html

Rails: How to set params on some paths with scope and set them to nil on some others?

I'm using some routes with scopes, and some without. See below.
resources :cities
resources :categories
devise_for :clients
namespace :clients do
resources :account
resources :dashboard
resources :offers
end
scope "/:current_city" do
scope "/:current_category" do
match 'articles/last_articles' => 'articles#index_last_articles', :as => "last_articles"
resources :articles do
resources :comments
end
end
end
root :to => "home#index"
I am using that params :current_city and :current_category and it gives me a URL like
http://localhost:3000/warszawa/all/articles/last_articles when I'm accessing articles.
**PROBLEM**
I have now such a problem that if I click on a link_to cities_path or root_path, then it adds those two parameters to the URL as http://localhost:3000/?current_category=all&current_city=warszawa.
I don't want these two parameters destroying the beauty of my URL :o(
The only way I found was to pass :current_city => nil, :current_category => nil for each link, but that's really heavy. I tried also the same but in my routes, which is working for normal resources, but not for namespaces, root_path, devise_for routes, and to be honest, that looks horrible in the routes.rb.
**QUESTIONS**
First I do not understand why these params are passed everywhere if I ask them only in the section with scope?!
Secondly, is there a way to make it work like I want or I should modify my routes?
I wish you understand my problem and if you have any comment or idea, please do not hesitate!
Thx

Devise: Unknown action, Could not find devise mapping for path "/accounts/sign_out"

I keep getting this error every time I point my browser to "account/sign_out" (GET request):
Unknown action, Could not find devise mapping for path "/accounts/sign_out"
Here's my route for devise:
devise_for :accounts, :controllers => { :registrations => :accounts }
It must be something trivial, but I don't get it. Documentation says devise already provides an action for signing out and binds it to this exact route "/accounts/sign_out". Please share with me what am I doing wrong?
The output of rake routes shows that the action is mapped:
destroy_account_session GET /accounts/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
The problem was that in routes.rb I also had resources :accounts route declared before devise_for. Therefore, the solution turned out to be to put it after the devise_for declaration:
devise_for :accounts, :controllers => { :registrations => :accounts }
resources :accounts

Resources