...

/

Responding with Rich Text

Responding with Rich Text

Learn to give access to the admins to respond to support requests

Allowing admins to respond

To allow our admins to respond to support requests, we’ll need to make a new UI for them to see the requests that need a response, a way for them to provide a response, and some code to email the customer back. We know how to do all of these things, so this is a great opportunity to learn about Action Text, which is a Rails library that allows us to easily provide a rich-text editing experience. We can use this to allow our admins to write a fully formatted response and not just plain text.

Let’s first quickly create the UI where we’ll see the support requests and edit them. This should be o familiar to us by now, so we’ll move quickly. Add a new route to config/routes.rb for the index() and update() methods:

Press + to interact
Rails.application.routes.draw do
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
resources :users
resources :products do
get :who_bought, on: :member
end
resources :support_requests, only: [ :index, :update ]
scope '(:locale)' do
resources :orders
resources :line_items
resources :carts
root 'store#index', as: 'store_index', via: :all
end
end

Creating controller for support request

Now, create app/controllers/support_requests_controller.rb and implement index(), like so (we’ll see update() in a moment):

Press + to interact
class SupportRequestsController < ApplicationController
def index
@support_requests = SupportRequest.all
end
def update
end
end

Next, we’ll create the view in app/views/support_requests/index.html.erb:

Press + to interact
<ul>
<% @support_requests.each do |support_request| %>
<li>
<h1>
On <%= support_request.created_at.to_formatted_s(:long) %>
<code><%= support_request.email %></code> writes:
</h1>
<p>
<blockquote>
<h2><%= support_request.subject %></h2>
<%= support_request.body %>
</blockquote>
</p>
<% if support_request.order %>
<h3>Recent Order</h3>
<dl>
<dt>Name</dt>
<dd><%= support_request.order.name %></dd>
<dt>Email</dt>
<dd><%= support_request.order.email %></dd>
<dt>Address</dt>
<dd><%= support_request.order.address %></dd>
<dt>PayType</dt>
<dd><%= support_request.order.pay_type %></dd>
<dt>Line Items</dt>
<dd>
<ul>
<% support_request.order.line_items.each do |line_item| %>
<li>
<%= line_item.product.title %>
(<%= line_item.product.price %>)
</li>
<% end %>
</ul>
</dd>
</dl>
<% else %>
<h3 class="notice">No associated order</h3>
<% end %>
<hr>
</li>
<% end %>
</ul>

Run the following application, create a few orders and, using the Rails conductor we saw earlier, create a few support tickets. Be sure at least one ...