You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dave Strus edited this page Nov 18, 2014
·
2 revisions
Listing Notes
Rather than having a pages#index action, we'll show a list of notes in the sidebar on every page when we're logged in.
Let's add a before_action to NotesController to grab all notes and assign the collection to an instance variable.
app/controller/notes_controller.rb
before_action:load_notes
app/controller/notes_controller.rb
defload_notes@notes=Note.allend
We'll use this instance variable in a partial.
_app/views/notes/list.html.erb
<% if @notes.present? %><ulid="notes"><%@notes.eachdo |note| %><li><%=content_tag:div,note.title,class: 'note-title'%><%=content_tag:div,note.body_html.to_s.html_safe,class: 'note-body'%></li><%end%></ul><%end%>
Let's render this partial in the sidebar in the application layout, which only logged in users will ever see. We'll get rid of the <div class="well"> element while we're at it.
app/views/layouts/application.html.erb
<navid="sidebar"><%=render'notes/list'%></nav>
For the moment, this list isn't clickable. We'll fix that shortly. Meanwhile, let's make a work-in-progress commit.
$ git add .
$ git commit -m "WIP List all notes in the sidebar."