Rails dynamic routes flattened - ruby-on-rails

I want to have something like this:
get '/received/:resouce' => 'received#index'
get '/received/:resouce/:resouce_id' => 'received#show'
post '/received/:resouce' => 'received#create'
put '/received/:resouce' => 'received#update'
delete '/received/:resouce' => 'received#delete'
get '/sent/:resouce' => 'sent#index'
get '/sent/:resouce/:resouce_id' => 'sent#show'
post '/sent/:resouce' => 'sent#create'
put '/sent/:resouce' => 'sent#update'
delete '/sent/:resouce' => 'sent#delete'
But this is very verbose. I want to use the scope and resources methods of routes for rails.
Any suggestions?

Got it!
scope '/sent/:resource' do
resources :sent, param: :resource_id, path: '/'
end
scope '/received/:resource' do
resources :received, param: :resource_id, path: '/'
end

Related

Hot to redirect "example.com/////" to just "example.com" in Rails app?

My goal is when user requests root (AND ONLY ROOT, other pages work fine) with something like:
http://domain.com/////
http://domain.com//////////////
http://domain.com//
app should 301 redirect user to one and only correct:
http://domain.com/
I'm using Webrick, rack-rewrite gem and planning to host it on Heroku.
Please remember that i'm new in Rails.
UPDATE ======
Rails: redirect all unknown routes to root_url
— this is totally different problem. They just redirect all unknown hosts to root. I have 404 working normally.
My problem is that any amount of slashes works as homepage and URL has all of those slashes. I'm doing it for SEO to get rid of duplicates of homepage. I want it to work like here: http://netpeak.ua (try "netpeak.ua////////", you will be redirected to "netpeak.ua").
UPDATE 2 - added content of routes.rb ======
Rails.application.routes.draw do
resources :questions
resources :feedbacks
devise_for :users
get '' => 'public#index'
get '/agency' => 'public#about'
get '/contact' => 'public#contact'
get '/uslugi' => 'services#index'
get '/portfolio' => 'projects#index'
get '/uslugi/:id' => 'services#show'
get '/portfolio/:id' => 'projects#show'
resources :articles, param: :id
resources :settings, as: 'home' #home so it doesn't go to another page
namespace :admin do
resources :articles, :users, :projects, :services, :feedbacks, :questions
get '' => 'projects#index'
get 'contact' => 'settings#contact'
get 'feedback' => 'settings#feedback'
get 'fininfo' => 'settings#fininfo'
get 'calls' => 'settings#calls'
get 'orders' => 'settings#orders'
get 'letters' => 'settings#letters'
get 'allquestions' => 'settings#allquestions'
get 'projectsettings' => 'settings#projectsettings'
get 'servicessettings' => 'settings#servicessettings'
get 'aboutsettings' => 'settings#aboutsettings'
get 'startpagesettings' => 'settings#startpagesettings'
patch 'contact' => 'settings#update'
patch 'feedback' => 'settings#update'
patch 'fininfo' => 'settings#update'
patch 'projectsettings' => 'settings#update'
patch 'servicessettings' => 'settings#update'
patch 'aboutsettings' => 'settings#update'
patch 'startpagesettings' => 'settings#update'
end
end

Rails 3.2. Routing helper generates unvalid links

In routes.rb:
scope "(:locale)", locale: /en|de/ do
get 'service' => 'service#index'
get 'service/:id' => 'service#show'
end
Then in view I use helper service_path(params[:locale], id) and get this link /en/service.1.
But i need link like this /en/service/1.
Check the routing via rake routes:
service GET (/:locale)/service(.:format) service#index (locale=>/en|ru/}
GET (/:locale)/service/:id(.:format) service#show {:locale=>/en|ru/}
How to get normal links like /en/service/1, what I'm doing wrong?
You should rename your controller to pluralize form Services
Change in routes:
get 'service' => 'service#index' to get 'services' => 'services#index', :as => :services
and
get 'service/:id' => 'service#show' to get 'services/:id' => 'services#show', :as => :service
Or you can write simple:
resources :services, :only => [:show, :index]

How can I pass additional parameters to a resource route in Rails?

I have a model called Post. In config/routes.rb, I defined its route as:
resources :post
Everything works fine with the default paths. I can create a new post at the following url:
/posts/new
I need to pass additional parameters so that the new url becomes:
/posts/new/:year/:month/:day
If I do the following, it assumes a post_id should exists:
resources :posts do
match '/new/:year/:month/:day',
:to => 'posts#new',
:constraints => {:year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/},
:as => 'new_post'
end
For the above, rake routes give me:
/posts/:post_id/new/:year/:month/:day(.:format)
How can I configure the default new path to pass additional parameters?
...
match '/new/:year/:month/:day', :on => :new
...

how to define rails 3.1 custom routes

I want to route http://localhost:3000/users/1/rename/alex to my users controller with rename action.
what I did was:
match 'users/:id/rename/:name' => 'users#rename', but this is not working, the part after 'users/:id/' is not mapped at all, since I cannot get name by params[:name]
Update:
In routes.rb
resources :users do
put 'rename/:code', :action => :rename, :code => /\w{5}/, :on => :member
end
and,
$ rake routes
...
PUT /users/:id/rename/:code(.:format) {:code=>/\w{5}/, :action=>"rename", :controller=>"users"}
...
If you have resources :users, put your match line before it.
Alternatively, you can pass a block to resources:
resources :users do
match 'rename/:name' => 'users#rename', :on => :member
end

Rails: what is wrong with this route?

For some strange reason cardsets_path('my') generates "/cardsets.my" instead of "/cardsets/my". Please explain why?
config/routes.rb:
match '/cardsets/:type', :to => 'cardsets#index', :requirements => { :type => /my|public/ }, :as => 'cardsets'
resources :users do
resources :cardsets do
end
end
rake routes:
cardsets /cardsets/:type(.:format) {:controller=>"cardsets", :action=>"index"}
Shouldn't it be
cardsets_path(:type => 'my')
However, type is a reserved word in rails.

Resources