Skip to content

Commit 4d278a9

Browse files
committed
updatedocs
1 parent 526aa30 commit 4d278a9

File tree

7 files changed

+77
-2
lines changed

7 files changed

+77
-2
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
11
# rails-module-railsadmin
22
> Rails module for rails-admin.
33
4+
5+
## step by step:
6+
+ Add new filed for admin user:
7+
```bash
8+
# gem 'devise'
9+
rails generate devise:install
10+
rails generate devise User
11+
12+
## Or you can add admin field to the migration
13+
# t.boolean :admin
14+
rails g migration AddAdminToUser
15+
```
16+
17+
+ Add auth to posts_contrlller:
18+
```ruby
19+
before_action :authenticate_user!
20+
```
21+
22+
+ check in home_controller:
23+
```ruby
24+
class HomeController < ApplicationController
25+
def index
26+
if current_user
27+
redirect_to posts_path
28+
end
29+
end
30+
end
31+
```
32+
+ Add to layout navations:
33+
```ruby
34+
<% if current_user %>
35+
<%= link_to 'logout' destroy_user_session_path, method: :delete%>
36+
<% else %>
37+
<%= link_to 'login' new_user_session_path %>
38+
<% end %>
39+
```
40+
41+
+ Install rails_admin
42+
```bash
43+
# gem 'rails_admin'
44+
rails g rails_admin:install
45+
```
46+
+ set authorization:
47+
```ruby
48+
config.authorize_with do
49+
redirect_to main_app.root_path unless warden.user.admin == true
50+
end
51+
```
52+
53+
+ result:
54+
```conf
55+
[email protected]/admin true ===> Can edit post/user
56+
other@qq.com /admin false ===> Can not ....
57+
```
58+
59+
460
## resources:
561
+ https://github.com/sferik/rails_admin

app/controllers/posts_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class PostsController < ApplicationController
2+
before_action :authenticate_user!
23
before_action :set_post, only: [:show, :edit, :update, :destroy]
34

45
# GET /posts

app/views/layouts/application.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
</head>
1010

1111
<body>
12+
13+
<% if current_user %>
14+
<%= link_to 'logout', destroy_user_session_path, method: :delete %>
15+
<% else %>
16+
<%= link_to 'login', new_user_session_path %>
17+
<% end %>
18+
1219
<%= yield %>
1320
</body>
1421
</html>

config/initializers/rails_admin.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
RailsAdmin.config do |config|
22

3+
4+
config.authorize_with do
5+
redirect_to main_app.root_path unless warden.user.admin == true
6+
end
7+
38
### Popular gems integration
49

510
## == Devise ==

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Rails.application.routes.draw do
22
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
33
resources :posts
4-
get 'home/index'
4+
# get 'home/index'
55

66
devise_for :users
7+
root 'home#index'
78
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
89
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class AddAdminToUser < ActiveRecord::Migration[5.0]
2+
def change
3+
end
4+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20170402141002) do
13+
ActiveRecord::Schema.define(version: 20170405010843) do
1414

1515
create_table "posts", force: :cascade do |t|
1616
t.datetime "created_at", null: false
@@ -30,6 +30,7 @@
3030
t.string "last_sign_in_ip"
3131
t.datetime "created_at", null: false
3232
t.datetime "updated_at", null: false
33+
t.boolean "admin"
3334
t.index ["email"], name: "index_users_on_email", unique: true
3435
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
3536
end

0 commit comments

Comments
 (0)