14 List Notes - getfretless/elevennote GitHub Wiki
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
def load_notes
@notes = Note.all
end
We'll use this instance variable in a partial.
_app/views/notes/list.html.erb
<% if @notes.present? %>
<ul id="notes">
<% @notes.each do |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
<nav id="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."