samedi 18 novembre 2017

rails adding /admin to routes when admin logs in

In my project I have a users model and an advertisements model. The users model contains the admins too. I want to set routes such that when an admin logs in, all URLs contain "/admin" in them. I am new to rails and having some trouble with the same. For example, when a regular user signs in, the url is /advertisements/:id/show but when an admin signs in, the URL becomes /admin/advertisements/:id/show. Tried a lot of methods but couldn't find any solution to do it. My User migration file is as follows:

class CreateUsers < ActiveRecord::Migration[5.1]

def change create_table :users do |t| t.string :first_name, limit: 15, null: false t.string :last_name, limit: 15 t.string :username, limit: 20 t.string :email, null: false t.string :password_digest t.boolean :approved, default: false t.boolean :admin, default: false t.timestamps end end end

My Advertisements migration file is:

class CreateAdvertisements < ActiveRecord::Migration[5.1]
  def change
    create_table :advertisements do |t|
      t.string :name, null: false
      t.text :description, null: false
      t.integer :price, null: false
      t.string :location, null: false
      t.integer :user_id
      t.boolean :approved, default: false
      t.timestamps
    end
    add_index("advertisements", "user_id")
  end
end

the routes file (routes.rb) has the code

resources: users
resources: advertisements

Is there any way to do it?

Aucun commentaire:

Enregistrer un commentaire