How to get alternative info from a routing namespace in rails? - ruby-on-rails

Imagine you are working on a f,acebook(to skip the g,f,w) like site, and you need some routes like:
www.mydomain.com/ihome/jim/posts
www.mydomain.com/ihome/jim/post/3
www.mydomain.com/ihome/jim/posts/3/edit.
Then how to set the routes to get the 'jim' part? I know I can use the following if there is no account part:
namespace :ihome do
resources :posts
end

A quick (untested) answer is : use the scope, it will give you a params[:user]
namespace :ihome do
scope ":user" do
resources :posts
end
end
Have a look at the docs here : http://guides.rubyonrails.org/routing.html#defining-defaults

Related

generate route to the namespace itself

I have multiple different namespaces like this:
namespace :some_category do
resources :orders
resources :some_foo
get 'aaa', to: 'aa#aa'
get 'bbb', to: 'bb#bb'
# etc
end
namespace :other_namespace do
resources :foo
resources :bar
get 'ccc', to: 'cc#cc'
get 'ddd', to: 'dd#dd'
# etc
end
namespace :etc do
end
Now, the routes are created correctly. Unfortunately, there aren't any routes generated for the namespaces itself. I would like to also have site.com/some_category and site.com/other_namespace.
should I just add outside the namespaces
get 'some_category', to: 'foo#bar'
get 'other_namespace', to: 'foo#baz'
Or is that bad practice?
I would like to make them as some kind of dashboards. Is it possible to generate links to all available subroutes?
I don't like to build a custom dashboard for each namespace. It's not very DRY, because all the dashboards are the same: it is just a list of links, that belongs in that namespace. Is there some code for:
Get the namespace of which I am the dashboard --> get all routes that are inside that namespace --> generate a link for each of those routes

How to shorten a url helper in rails

There are a lot of SO questions on this but none of them was able to answer what I was looking for. Please let me know if I am wrong.
I have following routes in my route files
namespace :integrations do
resources :google, only: [] do
collection do
get 'prompt'
get 'callback'
end
end
end
These creates very long url helpers like below.
prompt_integrations_google_index GET /integrations/google/prompt(.:format) integrations/google#prompt
callback_integrations_google_index GET /integrations/google/callback(.:format) integrations/google#callback
I've tried using as but that doesn't work. I don't know if using path is the right way. How can I shorten their helper name?
Ex:
google_prompt_integrations_google_index -> google_prompt
I would like to do the renaming for just one selected route and keep all others unchanged.
You can write the route definition as:
namespace :integrations, as: '' do
resource :google, only: [] do
collection do
get 'prompt'
get 'callback'
end
end
end
And then add the below inflections to remove _index with helper. _index comes from this implementation
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural /^(google)$/i, '\1s'
inflect.singular /^(google)s/i, '\1'
end
Now you get as you want:
$ rails routes | grep google
prompt_google GET /integrations/google/prompt(.:format) integrations/googles#prompt
callback_google GET /integrations/google/callback(.:format) integrations/googles#callback
Have you tried this way with as
namespace :integrations, as: '' do
resource :google, only: [:prompt, :callback] do
collection do
get 'prompt', as: 'prompt'
get 'callback', as: 'cb'
end
end
end
On rake routes we get prompt_google and cb_google
#rake routes
prompt_google GET /integrations/google/prompt(.:format) integrations/googles#prompt
cb_google GET /integrations/google/callback(.:format) integrations/googles#callback
Update:
In the case you want to just change 1 route why not have something like
get 'integrations/google/prompt', to: 'integrations/google#prompt', as: 'google_prompt'
rake routes will return
google_prompt GET /integrations/google/prompt(.:format) integrations/google#prompt

Rails 3 Routing

I have a community_users model that I route in the following way:
resources :communities do
resources :users
end
This creates the route /communities/:id/users/.
I'd like to configure this route so that only the name of the community with the corresponding :id is shown.
In other words, if a community has an id of '1' and the name 'rails-lovers' - the route would read:
/rails-lovers
and not:
/communities/1/users/
You might want to check out the gem friendly_id
That will give you the clean URLs you are looking for.
I'm not quite sure if this is what you're looking for, but:
One option would be to create the route
match ':community_name' => 'users#show_users_for_community'
and then in the UsersController have
def show_users_for_community
#community = Community.find_by_name(params[:community_name])
<do what you need to do here>
end
I'm not sure if that route will match too many URLs or not -- it's a very general route. So if you do this, maybe put it low down in your routes file.

Rails 3 Routing - specifying an exact controller from within a namespace

I'm having a bit of a problem with route namespaces that I've not encountered before. This is actually a part of some gem development I'm doing - but i've reworked the problem to fit with a more generic rails situation.
Basically, I have a namespaced route, but I want it to direct to a generic (top-level) controller.
My controller is PublishController, which handles publishing of many different types of Models - which all conform to the same interface, but can be under different namespaces. My routes look like this:
# config/routes.rb
namespace :manage do
resources :projects do
get 'publish' => 'publish#create'
get 'unpublish' => 'publish#destroy'
end
end
The problem is that this creates the following routes:
manage_project_publish GET /manage/projects/:project_id/publish(.:format) {:controller=>"manage/publish", :action=>"create"}
manage_project_unpublish GET /manage/projects/:project_id/unpublish(.:format) {:controller=>"manage/publish", :action=>"destroy"}
Which is the routes I want, just not mapping to the correct controller. I've tried everything I can think of try and allow for the controller not to carry the namespace, but I'm stumped.
I know that I could do the following:
get 'manage/features/:feature_id/publish' => "publish#create", :as => "manage_project_publish"
which produces:
manage_project_publish GET /manage/projects/:project_id/publish(.:format) {:controller=>"publish", :action=>"create"}
but ideally, I'd prefer to use the nested declaration (for readability) - if it's even possible; which I'm starting to think it isn't.
resource takes an optional hash where you can specify the controller so
resource :projects do
would be written as
resource :projects, :controller=>:publish do
Use scope rather than namespace when you want a scoped route but not a controller within a module of the same name.
If I understand you correct, you want this:
scope :manage do
resources :projects, :only => [] do
get 'publish' => 'publish#create'
get 'unpublish' => 'publish#destroy'
end
end
to poduce these routes:
project_publish GET /projects/:project_id/publish(.:format) {:action=>"create", :controller=>"publish"}
project_unpublish GET /projects/:project_id/unpublish(.:format) {:action=>"destroy", :controller=>"publish"}
Am I understanding your need correctly? If so, this is what Ryan is explaining.
I think what you want is this:
namespace :manage, module: nil do
resources :projects do
get 'publish' => 'publish#create'
get 'unpublish' => 'publish#destroy'
end
end
This does create the named routes as you wish(manage_projects...) but still call the controller ::Publish
It's a bit late but for anyone still struggling with this, you can add a leading slash to the controller name to pull it out of any existing namespace.
concern :lockable do
resource :locks, only: [] do
post "lock" => "/locks#create"
post "unlock" => "/locks#destroy"
end
end
Now if you include that concern anywhere (either namespaced or not) you will always hit the LocksController.

about rails route

if i use
namespace :helpcenter do
get "hh/logout"
get 'hh/login'
end
it will match the url helpcenter/hh/logout
my question is how to let these method mapping to the url /hh/logout didn't contains the module name
You can use a scope to achieve this:
scope :module => 'helpcenter' do
resources :articles
end
This will generate a mapping for /articles, /articles/new etc and not helpcenter/articles. It will however still route to the articles controller in the helpcenter namespace. e.g.: app/controllers/helpcenter/articles_controller.rb
Hope that helps.

Resources