Shallow routing in Rails 3 - ruby-on-rails

Having a rough time with my routes in my rails 3 app, I want to have shallow routes like this:
/san-francisco/union-square
But my router insists on having them like so:
/cities/san-francisco/neighborhoods/union-square
I've used this for my routes.rb
shallow do
resources :cities do
resources :neighborhoods do
resources :locations
end
end
end
But still I have this:
city_neighborhood_locations GET /cities/:city_id/neighborhoods/:neighborhood_id/locations(.:format)
Shouldn't it look like:
city_neighborhood_locations GET /:city_id/:neighborhood_id/:id(.:format)
I'm not sure how to fix this, additionally I'm not sure what I'm doing wrong with my links, I want to be able to use the syntax:
link_to neighborhood.name, [:city, neighborhood]
but that seems to invert the :id, and :neighborhood_id when the request comes to the controller, any help on this would be really really helpful!

What you're looking for is not typically known as "shallow routing".
Shallow routing, as Rails defines it, would look like this:
city_neighborhoods GET /cities/:city_id/neighborhoods
new_city_neighborhood GET /cities/:city_id/neighborhoods/new
(create_city_neighborhood) POST /cities/:city_id/neighborhoods
neighborhood GET /neighborhoods/:neighborhood_id
Without shallow routing, that last route would be:
city_neighborhood GET /cities/:city_id/neighborhoods/:neighborhood_id
Shallow routing lets you nest a resource underneath another resource (neighborhood under city in this case), but gives you absolute/un-nested routes for the nested resource when the nesting isn't necessary.
This makes sense when you're referencing the nested resource with unique identifiers that are not dependent on the ID of the parent resource. In your example, that's not true (there's potentially a "Union Square" outside of San Francisco; there's definitely going to be duplicates like "Chinatown"), so you probably do not want shallow routes for this case.
What you're wanting is positionally-dependent routing, where the type of resource is assumed/fixed depending on where it appears in the URL. (For instance, you couldn't have anything other than a "neighborhood" follow a "city" under the scheme you outlined.)
I don't think the Rails resource(s) commands will support that by default, but you could probably do it with manual match commands. This is off the top of my head:
match ":city_id", :controller => "cities", :action => "show"
match ":city_id/:neighborhood_id", :controller => "neighborhoods", :action => "show"
This is still RESTful/resource-based, it's just not using the standard Rails way of naming routes.

Related

Error using alias for routes in Ruby on Rails

I have a route with a namespace
namespace :publishers do
resources :authors
get 'books' :to => 'books'
get 'books/custom_report/:id', :to => "curriculos#custom_report"
end
Often I will have to make links in my application and I know than it`s possible to use a alias for routing like this:
<%= link_to "Books", publishers_books_path %>
I call that publishers_books_path a alias route, does this is the correct name?
Furthermore, I still not able to understand the logic with this alias naming because i can`t use for a new or a custom action like this
link_to 'Show the report', publishers_books_custom_report_path(params[:id])
I'm always get a error of undefined_method for publishers_books_custom_report_path
So there`s some questions
First of all whats it`s the correct name of this feature in RoR?
How I can use the custom_report as aliases to link_to? And also if i need to use some basic operations like new, update, insert?
Can someone give me the link to the documentation to really understant that feature?
First of all whats it`s the correct name of this feature in RoR?
The docs use "path helper" and "named route helpers" interchangeably.
How I can use the custom_report as aliases to link_to?
Use rails route or visit /rails/info/routes in your dev server to get a list of all your routes, their helpers, and controller actions.
Apparently it is publishers_path which doesn't seem right. You can fix this with an as.
get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
And also if i need to use some basic operations like new, update,
insert?
A get declares just that one specific route. If you need all the operations on a model, declare it as a resource.
namespace :publishers do
resource :authors
resource :books
get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
end
Can someone give me the link to the documentation to really understand that feature?
Rails Routing From The Outside In.

Nested routes in rails

I am someone who has always liked sinatra better than rails, and has never had to do a large enough scale project that rails was required (all the sources I have read say that rails is better for larger scale projects) and now I do have to do a large scale project. I have gotten very confused with the url structure of rails. What I am trying to do is the rails equivalent of this:
get "/" do
erb :index
end
get "/home" do
erb :dashboard
end
get "/home/profile" do
erb :profile
end
get "/home/friends" do
erb :friends
end
In the first one I understand that I should put in app/routes.rb I should put root home#index and in the home controller I should put def index end.
In the second one, I understand that I should do the same except replacing index with home.
But for the third and forth ones I have no idea what to do.
Also, is the a RESTful way to do the first two?
You probably want something like this
root 'home#index'
get 'home' => 'home#dashboard'
get 'home/profile' => 'home#profile'
get 'home/friends' => 'home#friends'
remember to use the command rake routes to see all your routes, where they lead and what their names are (if they have any)
I never understood what RESTful means, so someone else will have to answer that part of your question.
K M Rakibul Islam has shown you what can be called a "resourceful" way to do routes (because it uses the keyword resources) but it looks like you're just doing the static pages at this stage, so it's not necessary.
The simplest way to do routes is with this formula:
method url => controller::action, as: route_name
where method can be get, post, patch or delete so you can have different actions linked to the same URL depending on the method the request uses.
Putting a name on the route is optional, but it gives you a clean way to use the route in your views (route_name_path)
When you start making models then you'll find that using the resources keyword comes in handy. Read about it.
You can have this:
resources :home do
collection do
get :profile
end
collection do
get :friends
end
end
end
This will give you routes like this:
profile_home_index GET /home/profile(.:format) home#profile
friends_home_index GET /home/friends(.:format) home#friends
The standard way of declaring the root path:
root 'home#index'
And for the 2nd one, you have to do:
get 'home' => 'home#dashboard'
which will give you this route:
GET /home(.:format) home#dashboard
One route can be defined in many ways that works. But, Rails has conventions that should be followed while defining routes in your Rails app.
I would highly recommend you to take a look at the Rails Routing Guide

Ruby on Rails multiple methods per page in routes.rb?

Stupid question... I have two forms with two different functions on one page, my views/projects/new.html.erb file. So far I've only implemented one, with the option to "Create" a new project. I want to add another function to sort the records displayed on the same page, something like:
<%= link_to "Category", { :controller => "projects", :action => "sortTable", :filter => "Category" }, :remote => true %>
--
My routes.rb file:
Docside::Application.routes.draw do
resources :projects
resources :categories
#get "home/index"
root :to => "projects#new"
match 'project/new',:controller=>"projects",:action=>"create"
end
But I'm getting the error "No route matches {:action=>"sortTable", :controller=>"projects"}". When I tried adding " match 'project/new',:controller=>"projects",:action=>"sortTable" " my other function didn't work, and the create function got screwed up. What should I have instead?
Try that:
resources :projects do
collection do
post :sortTable
end
end
And look at this guide
You can only have one route for a given path and method combination. You're trying to define multiple routes on the same path, so only one of these will work (the first one). You should be ok if you use distinct paths for each of these actions (instead of project/new for all of them. Beware of collisions with your existing routes)
You'll also make you life easier if you stick to rails' conventions (and the code will be easier to read if someone else starts working on it). For example resources :projects already creates a route for the create action. Additional actions can be added like so
resources :projects do
collection do
get :sort_table
end
end
Sets up a collection route (ie one that isn't about a specific project) for the sort_table action and sets up a URL helper for you (sort_table_projects_path). There are alternative syntaxes you can use - I encourage you to have a look at the routing guide

Generate RESTful routes with dynamic prefix

I have an e-commerce web site written in Rails 3.2.8 which sells tickets for music events. So far I've been using simple RESTful routes in the application:
/ => default route: /events
/events
/events/1
/events/1/new
/events/2
...
All events used to happen in the same place, but from now on there's going to be two places, let's say "Morumbi" and "MaracanĂ£". Place is a model in the application, and it's a very important distinction between the events. So I'd like to make the place name a part of the path, and have routes like this:
/ => default route: page to choose place
/morumbi => same as /morumbi/events
/morumbi/events/1
/morumbi/events/1/new
/maracana => same as /maracana/events
/maracana/events/2
...
Although I know how to do that using the #match method, I have already a good number of routes created with the much more maintainable #resources method, and I'd like to keep them.
Do you know a solution avoiding the use of #match?
You need to use the #scope method:
scope path: ':place_name', as: 'place' do
resources :events
...
end
So GET /morumbi/events/1 will call EventsController#show with parameters id: '1' and place_name: 'morumbi'.

Rails 3 Routing resources scoped to a username

I have a basic understanding of rails routing, but nothing too advanced. So far I've gotten by using the RESTful resource based routes and a few custom named routes.
I am nearly done my app now though and I wanted to make some pretty urls.
In my app, each user has many pages. What's the best way to make the URL's look like www.sitename.com/username/page_name?
This will route to the pages controller's show action. Params hash includes :username and :page_name.
match "/:username/:page_name" => "pages#show"
Remember to put it last or it will match pretty much everything.
I'm not quite sure what you're using this for, but something like this might work in your routes file:
resources :users do
get 'page_name'
end
Which will produce: users/:id/page_name
You might want to check out the Railsguide on routing.
What you are looking for is a member route (section 2.9.1).
resources :users do
member do
get :cool_page
end
end
Will result in /users/:id/cool_page

Resources