Quantcast
Channel: User BvuRVKyUVlViVIc7 - Stack Overflow
Viewing all articles
Browse latest Browse all 42

Answer by BvuRVKyUVlViVIc7 for Rails - Dynamic query all records from has_many association with link_to

$
0
0

You should take a look at the named routes feature in Ruby on Rails: https://guides.rubyonrails.org/routing.html#path-and-url-helpers

Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of resources :photos:photos_path returns /photos

new_photo_path returns /photos/newedit_photo_path(:id) returns /photos/:id/edit (for instance, edit_photo_path(10) returns /photos/10/edit)photo_path(:id) returns /photos/:id (for instance, photo_path(10) returns /photos/10)

Each of these helpers has a corresponding _url helper (such as photos_url) which returns the same path prefixed with the current host, port, and path prefix.

So to be able to reach your controller, you have to add some routes to your routes.rb file and use it in your view:

# routes.rb  resources :buildings  resources :residents# index view<% @building.each do |b| %><div><%= link_to b.name, building_path(b) %></div><% end %># show view to show all residents, belonging to a building<% @building.residents.each do |resident| %><div><%= link_to resident.name, resident_path(resident) %></div><% end %>

Viewing all articles
Browse latest Browse all 42

Trending Articles