Reputation: 8454
In my Rails project, I have two different pages that exist within the same path - one is an index of stores, the other is a store update page (though I don't use those exact names for them). The problem that I am having is that I am trying to add dynamic links to the index page for each store to take a user to the associated update page, but I created the routes manually (rather than use :resources) and both pages are listed under the same path in my Rails routes summary. How can I use the <%= link_to %> helper in this situation?
First off, here is the relevant routing information...
stores_study_sites_path GET /stores/study_sites(.:format) stores#study_sites
GET /stores/store_details/:id(.:format) stores#store_details
And from my routes file...
get 'stores/study_sites' => 'stores#study_sites'
get 'stores/store_details/:id' => 'stores#store_details'
The first route, 'study_sites' is an index page, 'store_details' is the update page.
The redirect is rendered as a partial on the study_sites page...
<% @store.each do |store| %>
<ul>
<li>
<%= store.name %></br>
<%= store.street %></br>
<%= store.city %> <%= store.state %></br>
<%= render "shared/store_details", :store => store %>
</li>
</ul>
<% end %>
And finally, the partial that I would like to use, but does not currently work...
<%= link_to "Build store profile", stores_study_sites_path(store.id) %>
The urls generated from this look like http://localhost:3000/stores/study_sites.26
whereas I need them to be http://localhost:3000/stores/store_details/26
I've gone through this basic procedure for a number of other redirects with no problem, but I've created these custom urls/routes, and now I'm in a bit of a pickle. In a situation like this, how do I specify which url in the path I want the link to route to?
As a follow-up question (keep in mind I'm really new to Rails), why is the store_details page falling under the stores_study_sites_path?
Thank you very much.
Upvotes: 1
Views: 1790
Reputation: 76784
You really should put this setup within the context of your controller
in the routes:
#config/routes.rb
resources :stores do
get :study_sites, on: :collection #-> url.com/stores/study_sites
get :store_details, on: :member #-> url.com/stores/:id/store_details
end
... and then in the view:
<%= link_to "x", stores_store_sites_path %>
<%= link_to "y", stores_store_details_path(store.id) %>
Upvotes: 1
Reputation: 102249
When starting out with Rails try to avoid falling off the band wagon - most real world problems can be solved with the standard CRUD routes and RESTful representations. When you start creating a bunch of custom routes just for different representations then the quality tends to dip sharply.
A better URL scheme would be:
/stores # index of stores
/stores/1 # show a single store
/stores/1/details # index of details of a store.
/stores/1/details
is what you would call a nested resource. Its very clear from the URL that we are looking something which belongs to a store.
You can declare the routes with:
resources :stores, shallow: true do
resources :details
end
You can then create a link to the details of a store with:
<%= link_to "Build store profile", store_details_path(@store) %>
Upvotes: 3
Reputation: 1716
You can name your routes manually:
get 'stores/study_sites' => 'stores#study_sites', as: "first_route"
get 'stores/store_details/:id' => 'stores#store_details', as: "second_route"
Then use them:
<%= link_to "one", first_route_path %>
## this will generate http://localhost:3000/stores/study_sites
<%= link_to "two", second_route_path(5) %>
## this will generate http://localhost:3000/stores/store_details/5
Upvotes: 2