Rails routing with subfolders: namespaces and resources - ruby-on-rails

I have such route:
namespace admin
namespace :catalogs do
namespace :to do
resources :manufacturers do
resources :models do
resources :types do
resources :details
end
end
end
end
end
end
(please do not ask what's the model, why so man sub's: it must be so)
and such folder structure:
in manufacturer index view i have link:
= link_to "Подробнее", admin_catalogs_to_manufacturer_model_path(c), :class=>'btn btn-primary'
(also rake routes say that it is right route)
and when i try to render my index view i get:
No route matches {:action=>"show",
:controller=>"admin/catalogs/to/models",
:manufacturer_id=>#}
when i custom my link to :
= link_to "Подробнее", admin_catalogs_to_manufacturer_model_path(manufacturer_id: c.MFA_ID, id: c.MFA_ID)
i get error
uninitialized constant Admin::Catalogs::To::ModelsController
what is wrong? how to solve my routing?

Related

Rails says No route matches but I see it in `rake routes`

Rails says:
No route matches {:action=>"import", :controller=>"admin/users"}
but rake routes shows:
import_admin_user POST /admin/users/:id/import(.:format) admin/users#import
My routes.rb looks like:
namespace :admin do
resources :users do
member do
post :import
end
end
end
What am I missing? Rails 5.0.7
I got this working with:
namespace :admin do
resources :users do
collection do
post :import
end
end
end
Apparently collection is for actions that don't take an id.

Link_to pointing to static route

I've created a controller Pages and some actions for simple pages (contact us, for instance), then I went to routes.rb and created a route to allow users to go directly to /contactus, instead of /pages/contactus.
How can I point link_to to the action, but still getting the right route url?
get :contact_us, to: 'pages#contact_us'
or
get :contact_us, controller: :pages, action: :contact_us
this will generate path contact_us_path or url contact_us_url
HEARE MORE ABOUT ROUTES IN RAILS
#config/routes.rb
resources :pages, path: "", only: [] do #-> has to be above everything in routes file
collection do
get :contact_us #-> url.com/contact_us
get :about #-> url.com/about
end
end
root ...
You'd link to it as follows:
<%= link_to "Contact", pages_contact_us_path %>
You can do this:
get '/contactus', to: 'pages#contactus'
Your link can be:
<%= link_to "Contact Us", contactus_path %>
For more information, see: http://guides.rubyonrails.org/routing.html#connecting-urls-to-code
This is the syntax for a simple route using the contactus action in the pages controller:
get '/contactus' => 'pages#contactus'
or if you want a simpler name for your path:
get '/contactus' => 'pages#contactus', as: :contact

how to use subdomain with link_to rails

I have issue and I can't fix it.
This code in routes.rb
Rails.application.routes.draw do
namespace :admin do
constraints subdomain: 'admin' do
root to: "home#index"
concern :supportable do
resources :supports, only: [:new, :create]
end
resources :users, concerns: :supportable do
collection do
get 'search'
end
end
end
end
end
I want to use link_to in Ruby on Rail for link. example : <%= link_to admin_users_path do %> but in view show href="/admin/users". if I click to link redirect to http://admin.example.com/admin/users. But this link incorrect. I want to redirect link http://admin.example.com/users.
How to use link_to but render to html as href="/users".
Thanks,
Try this code with path:'/' in namespace, work for me:
Rails.application.routes.draw do
namespace :admin, path: '/' do
constraints subdomain: 'admin' do
...
end
end
end

Update a model column value by clicking a link ruby on rails

I have a servicebooking model and I am trying update its accept_booking column with a value of 1 when the following link is clicked in my servicebooking show view:
<%= link_to 'Accept this booking', acceptbooking_servicebooking_path(#servicebooking) %>
I get the following error:
undefined method `acceptbooking_servicebooking_path' for #<#<Class:0x5a35e98>:0x5a2bbf0>
Below I have declared the route in routes.rb
get 'acceptbooking', to: 'servicebookings#acceptbooking'
I have the following acceptbooking method in my servicebookings controller:
def acceptbooking
render nothing: true
#servicebooking = Servicebooking.find(params[:id])
#servicebooking.update_attribute(:accept_booking, 1)
end
my Routes.rb
Appa::Application.routes.draw do
devise_for :users
devise_for :views
get "welcome/index"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'welcome#index'
get 'myevents', to: 'events#myevents'
get 'myvenues', to: 'venues#myvenues'
get 'myservices', to: 'services#myservices'
get 'make_available', to: 'services#make_available'
get 'myservicebookings', to: 'servicebookings#myservicebookings'
get 'acceptbooking', to: 'services#acceptbooking'
#match 'acceptbooking', to: 'servicebookings#acceptbooking', via: [:get, :post]
resources :events do
resources :comments
end
resources :users do
resources :events
end
resources :venues do
resources :comments do
collection do
post :venuec
end
end
end
resources :services do
resources :comments do
collection do
post :servicec
end
end
end
resources :servicebookings do
resources :services
end
end
Can anyone see where Iam going wrong here? Thanks guys.
Try this: get 'acceptbooking/:id', to: 'servicebookings#acceptbooking', as: 'acceptbooking_servicebooking'
With just the route definition (from your route file):
get 'acceptbooking', to: 'servicebookings#acceptbooking'
the name of the route will be acceptbooking so you should be using acceptbooking_path. But based on your <%= link_to 'Accept this booking', acceptbooking_servicebooking_path(#servicebooking) %>, it appears like you want a url like /servicebookings/:service_booking_id/acceptbooking. If this is the case then you should update your route definition to:
resources :servicebookings do
resources :services
# Move the following line here from the top.
get 'acceptbooking', to: 'servicebookings#acceptbooking'
end
And this will give you servicebooking_acceptbooking_path (Note the order of servicebooking and acceptbooking here).

Post to Create Action Using link_to

I'm trying to post to my registration controller using a link_to link.
I have <%= link_to "Register for Period", registration_path(period_id: period.id), :method => :post %>
Which generates a link like: http://localhost:3000/registrations/6?period_id=25 where the 6 is the event_id. I need to save the period_id and the user_id to the registration database.
I get the following error in the browser: No route matches [POST] "/registrations/6"
What am I doing wrong?
routes:
Mavens::Application.routes.draw do
devise_for :users
resources :events
resources :periods
resources :products
resources :cart_rows
resources :product_requests
resources :inqueries
resources :registrations
match '/profile', to: 'static_pages#profile'
root :to => 'static_pages#home'
get "static_pages/home"
get "static_pages/about"
end
If you put in your routes.rb:
resources :registrations do
member do
post :save_period
end
end
And in your link:
<%= link_to "Register for period",
save_period_registration_path(id: #registration.id, period_id: period.id), :method => :post %>
You will have a route that matches your resquest.
When you only have a resources :registrations rule on your routes.rb, only the default restful routes are created, and there is no POST to a single resource created by default.
I believe you will have to read something about the CSRF token, because if you have a protect_from_forgery on your application_controller, probably this POST request from a single link would not work.
Your routes.rb is missing the required path. It IS a standard path, so adding 'resources :registrations' would work.
If more complex, post your routes.rb and we can tell what to add.

Resources