GithubHelp home page GithubHelp logo

rails_admin_slug's Introduction

RailsAdminSlug

A RailsAdmin field type to create a URL slug as you type a page title

What is it

Normally when using a gem like FriendlyId, the slug is created server side where the end user has no way of editing what the slug may be. Sure you can have a form field where they can fill in the slug if they so desire, or edit the slug after the model is created. This can lead to confusion and disuse with less technical users.

This gem brings the slug creation to the client side in RailsAdmin. The slug is automatically created when a user types the model title. From there the slug can be reviewed and edited before initial model creation.

Installing

Simply add the gem to your projects Gemfile after the rails_admin line.

gem "rails_admin_slug"

Create an app/assets/javascripts/rails_admin/custom/ui.js file and add the following line to the top (or just add if the file already exists):

//=require 'ra.slug'

Usage

In your RailsAdmin configuration file (usually config/initializers/rails_admin.rb) override the field type for the slug field. The "source" configuration option defines which field the slug will be tied to and is required to be set. (On the field line, the first :slug is your slug field, the second in the field type, overriding string in this case.)

RailsAdmin.config do |config|
  config.model 'Page' do
    edit do
      field :slug, :slug do
        source 'title'
      end
    end
  end
end

The slug field type extends the string field type. This means it is still possible to set other configuration items.

RailsAdmin.config do |config|
  config.model 'Page' do
    edit do
      field :slug, :slug do
        source 'title'
        help 'You may edit the slug if you wish'
      end
    end
  end
end

Credits

The jquery plugin that creates and edit the client side slug is from Patrick McElhaney and can be found at: https://github.com/pmcelhaney/jQuery-Slugify-Plugin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.