-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrubyonrails.txt
More file actions
258 lines (168 loc) · 4.86 KB
/
Copy pathrubyonrails.txt
File metadata and controls
258 lines (168 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
>>>>>> Ruby On Rails <<<<<<<<<<<
---------------------------------
---------------------------------
Keys: controller, model, view, simple rest api
---------------------------------
---------------------------------
---------------------------------
---------------------------------
RubyOnRais (Create Api's Commands):
Make the ApplicationController inherit from ActionController::API instead of ActionController::Base.
Models:
$ rails new rest-api-guide --api --database=postgresql
$ rails generate model User username:string password:string
$ rails generate model Fact user:references fact:string likes:integer
$ rails db:setup
$ rails db:migrate
Controllers:
$ rails g controller api/v1/Users
$ rails g controller api/v1/Facts
-------------------------------------
-------------------------------------
>>>>>>>>>>>>> All Templates <<<<<<<<<<<<<<<<<<<<<
Ruby On Rails:
--> ( Model ) <---
//app/models/article.rb
class Article < ApplicationRecord
belongs_to :author, default: -> { Current.user }
has_many :comments
has_one_attached :avatar
has_rich_text :content, encrypted: true
enum status: [ :drafted, :publised ]
scope :recent, -> { order(created_at: :desc).limit(25)}
after_save_commit :deliver_later, if: :publised?
def byline
"Written by #{author.name} on #{created_at.to_s(:short)}"
end
def deliver_later
Article::DeliveryJob.perform_later self
end
end
--> ( Controller ) <--
//app/controllers/articles_controller.rb
class ArticleController < ApplicationController
def index
@articles = Article.recent
end
def show
@article = Article.find(params[:id])
fresh_when etag: @article
end
def create
article = Article.create!(article_params)
redirect_to article
end
private
def article_params
params.require(:article).permit(:title, :content)
end
end
--> ( Views ) <--
//app/views/articles/show.html.erb
<h1><%= @article.title %></h1>
<%= image_tag @article.creator.avatar.url %>
<p><%= @article.content %></p>
<%= link_to "Edit", edit_article_path(@article) if Current.user.admin? %>
--> ( Routes ) <--
//config/routes.rb
Rails.application.routes.draw do
resource :article do # /articles, /article/1
resources :connents # /articles/1/comments, /comments/1
end
root to: "articles#index" # /
end
---------------------------------
---------------------------------
>>>> Rails 7 Simple Rest Api <<<<<<<
Table: users
Models:
rails generate model User
Controller:
rails generate controller User
rails routes
http://localhost:3000
http://localhost:3000/users
Model:
class User < ApplicationRecored
end
------------------
create_table "users", force :: cascade do |t|
t.string "username"
t.string "password"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
//User Controller
class UserController < ApplicationController
skip_before_action :verify_authenticity_token
#before_action :find_fact, only: [:show, :update, :destroy]
# GET /users
def index
@user = User.all() # find(x) # x = given id
render json: @user
end
# GET /users/id
def show
@user = User.find(params[:id])
if @user
render json: @user
else
render error: { error: 'Unable to create User.'}, status: 400
end
end
# POST /users
def create
@user = User.new(user_params)
if @user
@user.save
render json: @user
else
render error: { error: 'Unable to create User.'}, status: 400
end
end
# PUT /users/id
def update
@user = User.find(params[:id])
if @user
@user.update(user_params)
render json: { message: 'User successfully updated.'}, status: 200
else
render json: { error: 'Unable to update User.'}, status: 400
end
end
# DELETE /users/id
def destroy
@user = User.find(params[:id])
if @user
@user.destroy()
render json: { message: 'User successfully deleted.'}, status: 200
else
render json: { error: 'Unable to delete User.'}, status: 400
end
end
private
def user_params
params.require(:user).permit(:name, :email)
#params.permit(:name, :email)
end
#def contact
#redirect_to action: 'services', id: 2
#end
end
---------> End Conroller <----------------------------
Rails.application.routes.draw do
get '/users', to: "user#index"
get '/users/:id', to: "user#show"
get '/users/new', to: "user#new"
post '/users', to: "user#create"
get '/users/:id/edit', to: "user#edit"
put '/users/:id', to: "user#update"
delete '/users/:id', to: "user#destroy"
end
-- OR --
Rails.application.routes.draw do
resources :users #OR resource :user
end
---------------------------------------------------------------
---------------------------------
---------------------------------