'Rails: rendering a partial for a nested resource
# routes.rb
resources :topics do
resources :bookmarks, except: [:index]
end
The view files for my Bookmark model live in app/views/topics/bookmarks
:
I render a bookmarks partial from within views/topics/_topic.html.erb
:
...
<% if topic.bookmarks.count > 0 %>
<% topic.bookmarks.each do |bookmark| %>
<%= render partial: 'topics/bookmarks/bookmark', locals: { topic: topic, bookmark: bookmark } %>
<% end %>
<% end %>
...
My problems is that I'm receiving a Template Missing error when trying to render the Bookmark partial. Is the path I'm passing to render partial:
incorrect? I've also tried render partial: 'bookmarks/bookmark
.
Missing template bookmarks/edit, application/edit with {:locale=>[:en], :formats=>[:html], :variants=>[],
:handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
Searched in: * "/Users/..../dev/.../.../code/.../app/views" *
"/Users/.../.rvm/gems/ruby-2.2.1/gems/devise-3.5.1/app/views"
Here is the full stack trace: https://goo.gl/wd3oqQ
Solution 1:[1]
I think that your controller should follow the same structure that you want to use in your views folder, so your bookmarks controller should be defined like this:
class Topics::BookmarksController < ApplicationController
And it should be placed in the /controllers/topics/ folder.
You can see something related to that in Rails guide
Solution 2:[2]
I'm not sure if this is the best practice, but I tried do this, change from this:
<% topic.bookmarks.each do |bookmark| %>
<%= render partial: 'topics/bookmarks/bookmark', locals: { topic: topic, bookmark: bookmark } %>
<% end %>
to this:
<%= render partial: 'topics/bookmarks/bookmark', :collection=> @topic.bookmarks.all %>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Diego Senott |
Solution 2 | Terng Gio |