-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_Rails4
2194 lines (1508 loc) · 60.6 KB
/
start_Rails4
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--- NOTE: '$' means looged as a normal S.O. user and '#' means looged as root.
--- Following the instruction from:
https://www.server-world.info/en/note?os=CentOS_7&p=rails4
https://www.howtoforge.com/tutorial/how-to-install-and-use-ruby-on-rails-with-postgresql-on-centos-7/
https://www.youtube.com/watch?v=d8-V2V9oMvI&list=PLpOqH6AE0tNiQ-ofrDlbAUSc1r67r_AWv&index=2
##########################################################################################################################################
######
###### 1) Create a new App, using PostgreSQL and the Rails 4 version:
######
##########################################################################################################################################
$ rails _4.2.11.3_ new blog -d postgresql
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
. . .
Using web-console 2.3.0
Bundle complete! 12 Gemfile dependencies, 59 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
run bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
- Go inside the new project (App):
$ cd blog
- Config the DB and its conection:
$ vim config/database.yml
. . .
- Create the DB:
$ rake db:setup
$ rake db:migrate
##########################################################################################################################################
######
###### 2) Start the App server:
######
##########################################################################################################################################
$ rails s
Web --> http://localhost:3000
Ctrl+C
##########################################################################################################################################
######
###### 3) Create a controller (always *_controller.rb):
######
##########################################################################################################################################
$ rails generate controller welcome index
create app/controllers/welcome_controller.rb
invoke scss
. . .
create app/assets/stylesheets/welcome.scss
- Start server:
$ rails s
Web --> http://localhost:3000/welcome/index
Ctrl+C
- Route "welcome/index" (controller/action) as root view, in /config/routes.rb:
#get 'welcome/index'
root 'welcome#index'
- Can add style sheets (*.css) into /app/assets/stylesheets.
##########################################################################################################################################
######
###### 4) Create a model (always Cap first letter and singular, the table will have the same name, but non-cap and plural):
######
##########################################################################################################################################
$ rails generate model Article \
title:string \
body:text \
visits_count:integer
Running via Spring preloader in process 14675
invoke active_record
create db/migrate/20210223143952_create_articles.rb
create app/models/article.rb
invoke test_unit
create test/models/article_test.rb
create test/fixtures/articles.yml
. . .
##########################################################################################################################################
######
###### 5) Migrate: create the table into the DB
######
##########################################################################################################################################
$ rake db:migrate
- Inspect DB tables from Rails console, as objects (by its models):
$ rails console
irb> Article.all
##########################################################################################################################################
######
###### 6) Create the main Layout: common code for all the controllers views
######
##########################################################################################################################################
- Select the "Open Sans" Google fontype from www.google.com/fonts (Roboto for Android) and add it in the header section by a <link href="">
- Create the common styles for all the views in /app/assets/stylesheets/styles.scss (*.scss for Rails)
- Create a navbar in /app/views/layouts/application.html.erb (then it will be a partial)
- The <%= yield %> as the last line in the layout file will load any view file content when is called
##########################################################################################################################################
######
###### 7) Routes: actions that a given controller can do
######
##########################################################################################################################################
In the /config/routes.rb this line:
resource :articles
Is equivalent to all the CRUD actions for the articles controller (N is the id):
get "/articles" (action INDEX) https://localhost:3000/articles
post "/articles" (action CREATE) https://localhost:3000/articles
delete "/articles/:id" (action DESTROY / DELETE) https://localhost:3000/articles/N
get "/articles/:id" (action SHOW) https://localhost:3000/articles/N
get "/articles/new" (action NEW) https://localhost:3000/articles/new
get "/articles/:id/edit" (action EDIT) https://localhost:3000/articles/N/edit
patch "/articles/:id" (action UPDATE) https://localhost:3000/articles/N
put "/articles/:id" (action UPDATE) https://localhost:3000/articles/N
7.1) resources parameter:
- All the actions except delete:
resource :articles, except: :delete
- Only the create and show actions:
resource :articles, only: [: , :]
7.2) Comment several lines:
=begin
. . .
text to be commented
. . .
=end
##########################################################################################################################################
######
###### 8) Controllers:
######
##########################################################################################################################################
- Create controller file: /app/controller/articles_controller.rb
The index action will list all the items in the model and give them to its view through the class variable @articles
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
end
- Create controller view directory: /app/views/articles
- Create index view files: /app/views/articles/index.html.erb
<%= @articles.inspect %>
- Insert a register in table articles by Rails console:
$ rails console
irb> Article.create( title: "Primer articulo", body: "Bienvenidos a mi blog.", visits_count: 0 )
##########################################################################################################################################
######
###### 9) Send data to the controller: actions new and create
######
##########################################################################################################################################
- Through the class variable @articles in the controller file: /app/controller/articles_controller.rb
The data is not persistent (isn't committed in DB) until the save method is called.
def new
@article = Article.new
end
def show
@article = Article.find(params[:id]
end
def create
@article = Article.new(title: params[:article][:title], body: params[:article][:body])
if @article.save
redirect_to @article
else
render :new
end
end
- IF block explanation:
- OK --> shows the saved element (redirect_to command)
- NOK --> shows the initial view to repeat the action (render command)
- Form in the New view /app/views/articles/new.html.erb
<%= form_for(@article) do |f| %>
. . .
<%end%>
##########################################################################################################################################
######
###### 10) Validates (secure DB tables to avoid SQLi and any other DB requirement) in models files by MVC:
######
##########################################################################################################################################
- Avoid to insert null parameter:
validates :title, presence: true
- In the controller, @OBJECT.save method is OK when the validations are passed (see (9)).
- In the view, @OBJECT.errors.full_messages keeps the validation NOK messages, to be shown to the client.
##########################################################################################################################################
######
###### 11) Basic ActiveRecord: 1:N interface from the same model to any DB (sqlite3, posgreSQL...)
######
##########################################################################################################################################
$ rails console
- Count table registers:
irb> Article.all.count
irb> Article.all.size
- Select ONE register by default column value:
irb> Article.find(5)
- Select ONE register by other column value:
irb> Article.find_by(title:"Segundo articulo")
- Select all the coincidences:
irb> Article.where("title LIKE ?", "%articulo%")
- From the controller, instead of the model:
def show
@article = Article.where("id LIKE ? OR title LIKE ?", params[:id], params[:title])
end
- Select all the non-coincidences:
irb> Article.where.not("id = 1")
- Delete ONE register by default column value:
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
##########################################################################################################################################
######
###### 12) Strong params: allow access only to specific table columns
######
##########################################################################################################################################
- In the controller file, a private method to require the table and permit the columns:
private
def article_params
params.require(:article).permit(:title,:body)
end
- Then modify the Create method:
def create
@article = Article.new(article_params)
. . .
end
##########################################################################################################################################
######
###### 13) Partials: put all the common code from views in a single _action.html.erb file
######
##########################################################################################################################################
- Clean the common code in the view files of the given controller and substitute it by the render command:
/app/views/articles/new.html.erb
/app/views/articles/edit.html.erb
- Copy the common code (the form_for section) in a partial file: /app/views/articles/_form.html.erb
- Use local variables to pass values from view files to partial file:
/app/views/articles/new.html.erb
<div style="width: 80%; margin: 0 auto;">
<%= render "form", name: "Crear" %>
</div>
/app/views/articles/edit.html.erb
<div style="width: 80%; margin: 0 auto;">
<%= render "form", name: "Editar" %>
</div>
- The local variable should by initialized with a default value in the partial file:
/app/views/articles/_form.html.erb
<% name ||= "Crear" %>
<h1><%= name %> artículo</h1>
<%= form_for(@article) do |f| %>
. . .
</div>
<% end %>
- The Edit action isn't available yet due the changes aren't persisted into the DB. In the controller file:
def edit
@article = Article.find(params[:id])
end
- The Update method will do it, so it have to pass the validations:
def update
if @article.update(article_params)
redirect_to @article
else
render :edit
end
end
- IF block explanation:
- OK --> shows the saved element (redirect_to command)
- NOK --> shows the initial view to repeat the action (render command)
- The main view, /app/views/articles/index.html.erb , shoud be modified to show all the existing elements
<% @articles.each do |article| %>
<h1><%= link_to article.title, article %></h1>
<div>
<%= article.body %> - <%= link_to "Eliminar", article, class: "red", method: :delete %>
</div>
<% end %>
- Index view explanation:
- link_to article.title, article --> every title is a link to the article element itself, the Show action
- article.body --> body param shown
- link_to "Eliminar", article, method: :delete --> Eliminar is a link to the article element too, but the Delete action
- To offer the Edit action, the /app/views/articles/show.html.erb view should be modified:
<h1><%= @article.title %></h1>
<div>
<%= @article.body %>
</div>
<div>
<%= link_to "Editar", edit_article_path(@article) %>
</div>
##########################################################################################################################################
######
###### 14) User authentication (devise), part I of II
######
##########################################################################################################################################
- Install devise gem.
First insert the command in the Gemfile:
gem 'devise'
- From the app root path, execute the installation:
$ bundle install
- Generate the gem devise installators with the generator (g) subcommand:
$ rails g devise:install
- Check this installation steps:
Ensure you have flash messages in /app/views/layaout/application.html.erb
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
- Create a model to be used by devise:
$ rails g devise User
- A new entry is created into the /app/config/routes.rb
devise_for :users
- A new migrate file is created into /db/migrate and should be executed:
$ rake db:migrate
- The devise gem is functional. Here are the new URLs:
Login (sign_in): https://localhost:3000/users/sign_in
Create account (sign_up): https://localhost:3000/users/sign_up
- If the user is already logged in, a close session action should be offered.
In /app/views/layaout/application.html.erb insert a new <li> element into the navbar:
. . .
<li class="col-md">
Tecnología
</li>
<% if user_signed_in? %>
<li class="col-md">
<%= link_to "Cerrar sesión", destroy_user_session_path, method: :delete %>
</li>
<% end %>
##########################################################################################################################################
######
###### 15) User authentication (devise), part II of II
######
##########################################################################################################################################
- Move the navbar to a partial file.
- Create /app/views/layouts/partials
- Create a new partial file copying from /app/views/layaout/application.html.erb
Modified: /app/views/layaout/application.html.erb
. . .
<body>
<%= render "partials/navigation" %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
</html>
New: /app/views/layouts/partials/_navigation.html.erb
<header>
<nav class="be-red white large-padding">
<ul class="no-list row center-xs middle-xs">
<li class="col-md">
<h1 class="no-margin" id="logo">Mi Blog</h1>
</li>
. . .
<% if user_signed_in? %>
<li class="col-md">
<%= link_to "Cerrar sesión", destroy_user_session_path, method: :delete %>
</li>
<% else %>
<li class="col-md">
<%= link_to "Iniciar sesión", new_user_session_path %>
</li>
<li class="col-md">
<%= link_to "Crear cuenta", new_user_registration_path %>
</li>
<% end %>
</ul>
</nav>
</header>
- Create the views for the new actions:
$ rails g devise:views
- Improve the views style:
Login: /app/views/devise/sessions/new.html.erb
Create account: /app/views/devise/registrations/new.html.erb
- Modify the initial (root) view: /app/views/wellcome/index.html.erb
<% if user_signed_in? %>
<h1>Bienvenido, <%= current_user.email %></h1>
<% end %>
##########################################################################################################################################
######
###### 16) 1:N associations (FKs between models)
######
##########################################################################################################################################
- The articles table registers should be associated to the users whom created them.
- 1 user have N articles --> has_many :singular
- Every article belongs to 1 user --> belongs_to :plural
- Create the FK creation migration file:
$ rails generate migration add_user_id_to_articles user:references
- Edit the articles model file: /app/models/article.rb
class Article < ActiveRecord::Base
belongs_to :user
. . .
- Edit the users model file: /app/models/user.rb
class User < ActiveRecord::Base
. . .
has_many :articles
end
- Edit the article controller file to allow new registers with the user_id column: /app/controllers/articles_controller.rb
def create
@article = current_user.articles.new(article_params)
. . .
- After creating a new articles register, from the console we can access to the referenced info:
$ rails console
irb> Article.last.user
irb> Article.last.user.email
- Update the article views to include the user methods (and to avoid race conditions):
/app/views/articles/show.html.erb
<h1><%= @article.title %></h1>
<% unless @article.user.nil? %>
<p>
Escrito por: <%= @article.user.email %>
</p>
<% end %>
. . .
##########################################################################################################################################
######
###### 17) Callbacks (executed during a state change in the object lifecycle)
######
##########################################################################################################################################
- An object could be created, updated and destroyed, that's its lifecycle. Active Record identifies these transitions.
Callbacks are methods that get called at certain moments of an object's life cycle.
Could be specific (before_validation, after_save...), for models, or general (before_action), for controllers.
- Register the callback into the controller file: /app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
before_action :authenticate_user!, except: [:show, :index]
before_action :set_article, except: [:index, :new, :create]
. . .
- Write the code that will be executed with the callback as a private method into the controller file
(authenticate_user is a devise own method, set_article must be defined):
. . .
private
def set_article
@article = Article.find(params[:id])
end
- Refactor the code to avoid repeat action definitions (erase all the lines where ".find(params[:id])" is used):
def show
# @article = Article.find(params[:id])
end
. . .
def edit
# @article = Article.find(params[:id])
end
. . .
def destroy
# @article = Article.find(params[:id])
. . .
def update
# @article = Article.find(params[:id])
. . .
- Callbacks in model: /app/models/article.rb
class Article < ActiveRecord::Base
. . .
before_save :set_visits_count
private
def set_visits_count
self.visits_count ||= 0
end
- In the other hand, fixing the bug: the visits_count column should be initialized when a new article is created.
When all the existing articles are fixed, a new method must exists to autoincrement the column:
/app/models/article.rb
. . .
def update_visits_count
self.update(visits_count: self.visits_count + 1)
end
private
. . .
/app/controllers/articles_controller.rb
. . .
def show
@article.update_visits_count
end
##########################################################################################################################################
######
###### 18) Scaffold (all the previous steps together):
######
##########################################################################################################################################
- Generate an scaffold passing the model name, its FK's with other models and the its fields (only one: body):
$ rails g scaffold Comment user:references article:references body:text
- Run the generated migration:
$ rake db:migrate
- And customize the new files:
/app/controllers/comments_controller.rb
lass CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
. . .
- Clean the new stylesheet:
$ > /app/assets/stylesheets/comments.scss
##########################################################################################################################################
######
###### 19) Nested resources
######
##########################################################################################################################################
- The comments in the blog are always related to article. So the comments resources should be related to the articles resources too:
/config/routes.rb
Rails.application.routes.draw do
resources :articles do
resources :comments
end
devise_for :users
root 'welcome#index'
end
- Clean index view: /app/views/comments/index.html.erb
$ > /app/views/comments/index.html.erb
- The comments should be shown from the articles view: /app/views/articles/show.html.erb
. . .
<div>
<%= @article.body %>
</div>
<div class="field">
<h3>Comentarios</h3>
<%= render "comments/form" %>
</div>
<ul>
<% @article.comments.each do |comment| %>
<li>
<%= comment.body %> - <%= comment.user.email %>
</li>
<% end %>
</ul>
</div>
. . .
- The article Show action must include a comment New action: /app/controllers/articles_controller.rb
def show
@article.update_visits_count
@comment = Comment.new
end
- The 1:N association in the article model: /app/models/article.rb
class Article < ActiveRecord::Base
belongs_to :user
has_many :comments
. . .
- Edit the comments form partial to be rendered by the articles show view: /app/views/comments/_form.html.erb
- The form_for command expect the @article resource as a parent and the @comment resource as a child.
<%= form_for([@article, @comment]) do |f| %>
. . .
- The article and user ids shouldn't be available
. . .
</div>
<% end %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
. . .
- From here, the relationship between comments and users:
- Define the 1:N association in the user model too: /app/models/user.rb
. . .
has_many :articles
has_many :comments
end
- The 1:N association in the controller: /app/controllers/comments_controller.rb
def create
@comment = current_user.comments.new(comment_params)
@comment.article = @article
. . .
- The callback: /app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_action :set_comment, only: [:update, :destroy]
before_action :set_article
. . .
private
def set_article
@article = Article.find(params[:article_id])
end
- Every time the @comment is metioned, it should be substituted by its parent (save and update actions):
/app/controllers/comments_controller.rb
. . .
if @comment.save
format.html { redirect_to @comment.article, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment.article }
. . .
if @comment.update(comment_params)
format.html { redirect_to @comment.article, notice: 'Comment was successfully updated.' }
format.json { render :show, status: :ok, location: @comment.article }
. . .
- The redirection in the Destroy action:
. . .
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to @article, notice: 'Comment was successfully destroyed.' }
. . .
- Allowing only certain access to the comments will be managed as routes: /config/routes.rb
. . .
resources :articles do
resources :comments, only: [:create, :destroy, :update]
end
- Clean the discarded comment actions (and callbacks) in the comment controller file: /app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_action :set_comment, only: [:update, :destroy]
before_action :set_article
before_action :authenticate_user!
. . .
# def index
. . .
# def show
. . .
# def new
. . .
# def edit
. . .
##########################################################################################################################################
######
###### 20) Remote forms with AJAX: the post actions will be shown without reload the entire web
######
##########################################################################################################################################
- Install jquery-turbolinks gem from Gemfile:
. . .
# Remote forms with AJAX
gem 'jquery-turbolinks'
. . .
$ bundle install
$ rails s
- Enable remote and JSON in the comments form: /app/views/comments/_form.html.erb
<%= form_for([@article, @comment], remote: true, html: { id: "comments-form", :"data-type" => "json" }) do |f| %>
. . .
- Redefine the article Show action to manage the ul element by AJAX:
. . .
<%= render "comments/form" %>
<ul id="comments-box">
<% @article.comments.each do |comment| %>
<li>
. . .
- Insert the AJAX commands in the Coffee script file: /app/assets/javascripts/comments.coffe
$(document).on "ajax:success", "form#comments-form", (ev,data)->
console.log data
$(this).find("textarea").val("")
$("#comments-box").append("<li> #{data.body} - #{} </li>")
$(document).on "ajax:error", "form#comments-form", (ev,data)->
console.log data
- When the form call is successfull, identified by its route (action#html_id), the DOM is managed by an AJAX function: (ev,data)->
- Show the output in the console (the output is a JSON object, data-type = JSON)
- Clean the textarea of the comment
- Write a new list element (into the ul "#comments-box" element), like the article Show action does.
It lets the user to check its comment just when he writes it
##########################################################################################################################################
######
###### 21) JSON with JBuilder
######
##########################################################################################################################################
- The JSON objects can be constructed by code, with the *.json.jbuilder view files: /app/views/comments/_comments.json.jbuilder
json.extract! comment, :id, :user_id, :article_id, :body, :created_at, :updated_at
json.user do
json.email @comment.user.email
end
- The JSON comment object has a new element, a subJSON key:value into the user element, refering to the user's email.
- Now thee AJAX code can be refactorized to use the new JSON element: /app/assets/javascripts/comments.coffe
. . .
$("#comments-box").append("<li> #{data.body} - #{data.user.email} </li>")
. . .
##########################################################################################################################################
######
###### 22) Upload files, part I of II
######
##########################################################################################################################################
- Install the paperclip gem with bundle from Gemfile:
. . .
# Allow to upload and edit image files to the server
gem 'paperclip'
. . .
$ bundle install
$ rails s
##########################################################################################################################################
######
###### 23) Upload files, part II of II
######
##########################################################################################################################################
- Create a migration file with the cover generator (with the server stopped), the fields used by the paperclip gem:
$ rails g migration add_cover_to_articles
- Edit the migration file to add the cover fields into the articles table: /db/migrate/20210510152452_add_cover_to_articles.rb
class AddCoverToArticles < ActiveRecord::Migration
def change
add_attachment :articles, :cover
end
end
- Execute the migration:
$ rake db:migrate
- Edit the Article model file to get the chance to access to the new fields (and validate that is a image file): /app/models/article.rb
. . .
has_attached_file :cover, styles: { medium: "1280x720", thumb: "800x600" }
validates_attachment_content_type :cover, content_type: /\Aimage\/.*\Z/
. . .
- Callbacks explanation:
- The cover reference is the metadata of the attached file.
- The paperclip gem will generate two new versions of the image, the medium and the thum ones.
- The model will validate that is any image file type (jpg, gif, etc.)
- Include a new field into the article form file to attach an image file: /app/views/articles/_form.html.erb
. . .
<%= f.text_field :title, placeholder: "Titulo", class: "form-control" %>
</div>