fullstackplus
fullstackplus

Reputation: 1071

Super-simple updates app in Rails

I have to cook up a super-simple Rails app with the following functionality:

Only one model: Update. Updates are simply news that my client will be publishing on the site, i.e. basically mini blog posts. An update has three fields: headline, text (both mandatory) and image.

The client will need to log in to create, update, & delete updates. No roles and authorization levels are necessary — just a single login / password combination for the above tasks.

It's desirable to have a very basic WYSIWYG (for inserting links, etc) in the UI for creating & editing updates.

It's been a while I've done something similar, so the question is: is it advisable just to roll everything from scratch or are there gems I should consider?

(I'll be probably using AWS and CarrierWave for images).

Much thanks!

Upvotes: 0

Views: 30

Answers (2)

Richard Peck
Richard Peck

Reputation: 76784

Simplest way to achieve this (and yes, it will be relatively simple) will be to use the Devise gem with InheritedResources. Here's how:


Flow

Firstly, you'll want to ensure you're able to give the use a place to compose & submit the updates. To do this, you'll want to create a simple backend (admin) system, to give you a separate space to update your backend:

#config/routes.rb
namespace :admin do
   resources :updates, path: "", except: :show #-> domain.com/admin/
end

root: "updates#index"
resources :updates, path: "", only: [:show, :index] #-> domain.com/:id

The importance of this type of system is relevant.

The issue I think you're alluding to is the way in which you won't be able to either create or access the various mini-updates from a "protected" area. Using the method I'm detailing will only only provide this area, but also give you the ability to store the data correctly.

--

Controller

To get this to work, you'll need two controllers:

#app/controllers/updates_controller.rb #-> for general users
class UpdatesController < InheritedResources::Base
   #InheritedResources will create relevant CRUD resources
end

#app/controllers/admin/updates_controller.rb #-> for admin
class Admin::UpdatesController < InheritedResources::Base
  before_action :authenticate_user! #-> for Devise
  #InheritedResources creates relevant responses
end

The above two controllers are really the core of what you need. They provide the CRUD functionality for both the admin and `general areas of your app - giving you the ability to provide the users with the ability to upload as they require.

The biggest thing you need to consider is the authentication. This is handled with Devise (which I'll explain below). The thing you need to consider is the authenticate_user! call - this is what determines whether the user is able to invoke the methods, depending on whether they are logged in (maintain an active session) or not

--

Devise (authentication)

The Devise gem will be what you need to get the authentication of your app sorted. This is what you're asking about -- and simply, it's a job for Devise.

There is a great tutorial on how to handle this here:

enter image description here

The bottom line with Devise is that you need 3 things to get it working:

  1. A User model
  2. A users table
  3. The Devise columns & system installed

Without over-burdening you, I'd just recommend using the controllers I put up above (with the corresponding views), the routes, and then Devise

Upvotes: 1

maxshelley
maxshelley

Reputation: 166

It's not a gem, but you could look at Rails Composer: https://github.com/RailsApps/rails-composer - they have templates for generating apps that meet your needs. It lets you set options to help you customise them further.

Upvotes: 1

Related Questions