1

I'm searching for the best way to set up a web project I'm working on.

My website is asking for users and non-users to input information about local locations in their towns. I don't want to restrict this to users only. I have a form to collect information about what state and city they are in, as well as details about their location. I'd like to approve this information before posting it to the website.

What is the best way to process the info? Does Rails have a good solution for me to approve this info before making it live?

I need a method to restrict data/listing before showing in a listing page.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Chelsea
  • 65
  • 1
  • 10

2 Answers2

0

You can use a flag attribute in your Model, preferably status to indicate whether it's approved or not. By default when a user add a data it should be false. You can manually update it to true to make that data live.

Eg. Assume you are adding a listing, it won't appear in your listing page because it's unapproved by default. You should write your listing controller like this,

@listing = Listing.where("status is not false")

Your controller create method should have below code to make sure every listing is not approved by default when it's added.

def create
  listing.status = false
  listing.save
end

Or you can use migration and add a default value to status as fault. source.

You can review it and make changes in your own Approval controller or by using a admin panel gem. RailsAdmin.

Community
  • 1
  • 1
Albert Paul
  • 1,168
  • 13
  • 23
  • Thank you! If I'm following you, I add in the user's submissions to the database but make sure they are all set to false. I make live all listings which are set to true. Then I can use the RailsAdmin gem to review submissions. – Chelsea Mar 01 '16 at 18:43
  • Thanks! I'll check out RailsAdmin and go from there and adjust my locations table. – Chelsea Mar 01 '16 at 18:49
  • Done, I believe. I'm new to all this. Appreciate your input! – Chelsea Mar 01 '16 at 18:58
0

This is a pretty general question, and will involve more than just simple Rails actions. It also depends on what you mean by approve this information. Do you as the administrator just want to make sure it is valid info (e.g. the user didn't type a bunch of random characters in a phone number field) or do you need to actually make sure the information itself is acceptable (e.g. the user didn't enter a bunch of profanity into a text box)?

For the first part, you can use Active Record Validations to make sure the data itself is sound (like verifying that a phone number actually consists of numbers).

For the second part, you will need to track whether or not the information should be displayed on your site. I don't know what data model you've set up, but let's say that the entirety of what the user is submitting is called a review.

To track whether or not this review should be displayed, you will want to add an approved attribute to the Review model. You can do this through an Active Record Migration. When a new review is created, approved should be false by default. Once you are ready to show this new review, you can set approved to true.

You can accomplish all of this through a few controller actions. I'll assume you have an admin flag on your personal user account so that you can approve reviews. If so, you could set up a controller like so:

class ReviewController < ActionController::Base
  def index
    if current_user.admin
      # Allow yourself to view all Reviews, presumably with an `Approve` button
      @reviews = Review.all
    else
      # Only allow standard users to see already-approved reviews
      @reviews = Review.where(approved: true)
    end

    # render reviews however you need to
  end

  def update
    @review = Review.find(params[:id])
    if params[:approved] && current_user.admin
      # You've clicked an approve button, and you are an admin
      @review.update_attribute(:approved, true)
    end

    # respond to the request here

    # Note, there are definitely cleaner ways to do this, but I wanted to
    # demonstrate where you could set the approved flag
  end
end
Jack Collins
  • 1,145
  • 10
  • 21
  • Thanks! What I mean by approve is primarily to make sure the info is relevant to the website, make sure it's not profanity or bot-created, or possibly malicious. – Chelsea Mar 01 '16 at 18:59
  • Your code helps set the example. There are a lot of gems out there and honestly, while I'm still learning, I'd like to do this myself in a simple fashion. What you're showing me seems straight-forward and basic. Thank you. – Chelsea Mar 01 '16 at 19:05
  • No problem! I have used various `Admin` type gems before, but if you're learning I definitely recommend writing this all from scratch until it becomes second nature. – Jack Collins Mar 01 '16 at 19:17
  • As far as an admin flag on my personal user, I have a child table called role which has a boolean under admin. I believe this should work, but would it be better to have this right on the user table? – Chelsea Mar 01 '16 at 19:24
  • That depends on where you think your application will go. If there will be many different user roles, and if those different user roles have substantially different behavior, then having a `role` table is a great plan. If, however, you only have two roles (admin or standard user), then moving that flag to the `user` table itself will likely be a better option. – Jack Collins Mar 01 '16 at 19:30