Published by Dan Cunning on Feb 21, 2015
This article builds on top of the Mailing Lists functionality.
Since we're storing all the messages in the database, let's display the full history of group discussions within our application for new and old members to browse:
# config/routes.rb
resources :groups, only: "index" do
resources :discussions, only: %w[index show]
end
# app/controllers/groups_controller.rb
class GroupsController < ApplicationController
def index
@groups = Group.order(:name)
end
end
# app/controllers/discussions_controller.rb
class DiscussionsController < ApplicationController
before_action :load_group
def index
@discussions = @group.discussions.order(created_at: :desc)
end
def show
@discussion = @group.discussions.find(params[:id])
@messages = @discussion.messages.order(:created_at)
end
private
def load_group
@group = Group.find(params[:group_id])
end
end
<!-- app/views/groups/index.html.erb -->
<ul>
<% @groups.each do |group| %>
<li><%= link_to group.name, group_discussions_path(group) %></li>
<% end %>
</ul>
<!-- app/views/discussions/index.html.erb -->
<h1><%= @group.name %></h1>
<ul>
<% @discussions.each do |discussion| %>
<li><%= link_to discussion.subject, group_discussion_path(@group, discussion) %></li>
<% end %>
</ul>
<!-- app/views/discussions/show.html.erb -->
<h1><%= @discussion.subject %></h1>
<% @messages.each do |message| %>
<p>From: <%= message.from.name %></p>
<p>Timestamp: <%= message.created_at.to_fs(:timestamp) %></p>
<%= simple_format message.content %>
<% end %>
I'm a Ruby on Rails contractor from Atlanta GA, focusing on simplicity and usability through solid design. Read more »