Related cards:
Nested controller routes (Rails 2 and 3)
In order to keep the controllers directory tidy, we recently started to namespace controllers. With the :controller
option you can tell Rails which controller to use for a path or resource. For nested resources, Rails will determine the view pat...
Rails routes: Extracting collection actions into their own controllers
Let's say you have two screens:
- Show a given project
- Show a report for all projects
Ideally you want both screens to be handled by different controllers like this:
GET /projects/:id => ProjectsController#show
GET /projects/...
Rethinking Rails 3 Controllers and Routes | Free PeepCode Blog
The Rails router has been written and rewritten at least four times2, including a recent rewrite for the upcoming Rails 3. The syntax is now more concise.<br />
<br />
But never mind making it shorter! It’s time for a final rewrite: Le...
Updated: Unpoly + Nested attributes in Rails: A short overview of different approaches
With the new unpoly client side templates (available since 3.10) there's another way to substitute the ids of inserted nested attribute forms which I added to this card.
A simpler default controller implementation
Rails has always included a scaffold
script that generates a default controller implementation for you. Unfortunately that generated controller is unnecessarily verbose.
When we take over Rails projects from other teams, we often find that cont...
Rails route namespacing (in different flavors)
TL;DR There are three dimensions you can control when scoping routes:
scope module: 'module', path: 'url_prefix', as: 'path_helper_name' do
resources :examples, only: :index
end
as
→ prefixes path helpers: `path_helper_name_ex...
Popular mistakes when using nested forms
Here are some popular mistakes when using nested forms:
- You are using
fields_for
instead ofform.fields_for
. - You forgot to use
accepts_nested_attributes
in the containing model. Rails won't complain, but nothing will work. In particular...
Define a route that only responds to a specific format
You won't usually have to do this. It's OK to route all formats to a controller, and let the controller decide to which format it wants to respond.
Should you for some reason want to define a route that only responds to a given format, here is ho...
How to disable logging for ActiveStorage's Disk Service routes
In development, we store files using ActiveStorage's disk
service. This means that stored files are served by your Rails application, and every request to a file results in (at least!) one non-trivial log entry which can be annoying. Here is how...
Generate a path or URL string from an array of route components
When using form_for
you can give the form's target URL either as a string or an array:
form_for(admin_user_path(@user)) do ... end
# same as:
form_for([:admin, @user]) do ... end
Same for link_to:
link_to("Label", edit_admin_u...