{"id":2689,"date":"2022-09-14T18:05:50","date_gmt":"2022-09-14T11:05:50","guid":{"rendered":"https:\/\/www.marketenterprise.vn\/blog\/?p=2689"},"modified":"2022-09-14T18:05:50","modified_gmt":"2022-09-14T11:05:50","slug":"3-filters-trong-ruby-on-rails","status":"publish","type":"post","link":"https:\/\/www.marketenterprise.vn\/blog\/3-filters-trong-ruby-on-rails.html","title":{"rendered":"Better Rails: 3 Filters gi\u00fap Controller c\u1ee7a b\u1ea1n sang x\u1ecbn m\u1ecbn h\u01a1n r\u1ea5t nhi\u1ec1u"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"><strong>Bonus:<\/strong> Trong b\u00e0i vi\u1ebft, ch\u00fang ta s\u1ebd d\u00f9ng m\u1ed9t ch\u00fat <a href=\"https:\/\/www.toptal.com\/ruby\/ruby-metaprogramming-cooler-than-it-sounds\" target=\"_blank\" rel=\"noopener\"><strong>metaprogramming<\/strong><\/a> \u0111\u1ec3 t\u1ea1o ra \u0111o\u1ea1n code gi\u00fap b\u1ecf \u0111i r\u1ea5t nhi\u1ec1u \u0111o\u1ea1n code l\u1eb7p \u0111i l\u1eb7p l\u1ea1i nh\u00e9.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trong b\u00e0i vi\u1ebft ch\u00fang ta s\u1ebd \u0111i qua c\u00e1c ph\u1ea7n sau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">T\u00ecm hi\u1ec3u v\u1ec1 Filters trong Rails,<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00c1p d\u1ee5ng Filters \u0111\u1ec3 share code gi\u1eefa c\u00e1c action trong c\u00f9ng m\u1ed9t Controller<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng metaprogramming \u0111\u1ec3 share code gi\u1eefa c\u00e1c controller<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u th\u00f4i. Let&#8217;s go.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">3 lo\u1ea1i filters trong Rails<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">\u0110\u1ea7u ti\u00ean ch\u00fang ta s\u1ebd n\u00f3i v\u1ec1 t\u00ednh n\u0103ng Filters m\u00e0 Rails cung c\u1ea5p. Rails cung c\u1ea5p cho ch\u00fang ta 3 lo\u1ea1i filters:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>before_filter<\/code>: ch\u1ea1y tr\u01b0\u1edbc khi request \u0111\u01b0\u1ee3c g\u1eedi \u0111\u1ebfn action trong controller<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>after_filter<\/code>: ch\u1ea1y sau khi action x\u1eed l\u00fd request<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>around_filter<\/code>: b\u1ecdc xung quanh action, ch\u1ee7 y\u1ebfu s\u1eed d\u1ee5ng cho x\u1eed l\u00fd ngo\u1ea1i l\u1ec7 (exception)<\/span><\/li>\n<\/ul>\n<figure id=\"attachment_2691\" aria-describedby=\"caption-attachment-2691\" style=\"width: 1242px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2022\/09\/11123438\/rails-3-filters.png\" alt=\"3 filters trong Rails\" width=\"1242\" height=\"930\" class=\"wp-image-2691 size-full\" srcset=\"https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2022\/09\/11123438\/rails-3-filters.png 1242w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2022\/09\/11123438\/rails-3-filters-300x225.png 300w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2022\/09\/11123438\/rails-3-filters-1024x767.png 1024w, https:\/\/mevn-public.s3-ap-southeast-1.amazonaws.com\/marketenterprise.vn\/wp-images\/2022\/09\/11123438\/rails-3-filters-768x575.png 768w\" sizes=\"auto, (max-width: 1242px) 100vw, 1242px\" \/><figcaption id=\"caption-attachment-2691\" class=\"wp-caption-text\">3 filters trong Rails<\/figcaption><\/figure>\n<p><span style=\"font-weight: 400;\">C\u00f9ng t\u00ecm hi\u1ec3u k\u1ef9 h\u01a1n nh\u00e9.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\"><code>before_filter<\/code><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u00e2y l\u00e0 Filter \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn nh\u1ea5t. C\u00f3 2 c\u00e1ch \u0111\u1ec3 g\u1ecdi m\u1ed9t <code>before_filter<\/code>.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1ch th\u1ee9 nh\u1ea5t l\u00e0 s\u1eed d\u1ee5ng m\u1ed9t anonymous block:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">class ArticlesController &lt; ApplicationController\r\n\r\n\u00a0\u00a0before_filter do\r\n\r\n\u00a0\u00a0\u00a0\u00a0@article = Article.find(params[:id]) if params[:id]\r\n\r\n\u00a0\u00a0end\r\n\r\n\u00a0\u00a0#...\r\n\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">\u0110o\u1ea1n code b\u00ean tr\u00ean s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi tr\u01b0\u1edbc t\u1ea5t c\u1ea3 request \u0111\u1ebfn b\u1ea5t k\u00ec action n\u00e0o b\u00ean trong controller. V\u1eady n\u1ebfu nh\u01b0 ch\u1ec9 mu\u1ed1n \u00e1p d\u1ee5ng \u0111o\u1ea1n code \u0111\u00f3 cho action <code>new<\/code> v\u00e0 <code>edit<\/code> th\u00f4i th\u00ec l\u00e0m th\u1ebf n\u00e0o? M\u1ed9t ch\u00fat n\u1eefa ch\u00fang ta s\u1ebd n\u00f3i \u0111\u1ebfn c\u00e1ch \u0111\u1ec3 scope ph\u1ea1m vi c\u1ee7a filter.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1ch vi\u1ebft th\u1ee9 hai sau \u0111\u00e2y l\u00e0 c\u00e1ch m\u00e0 m\u00ecnh \u01b0a th\u00edch h\u01a1n v\u00ec s\u1ef1 g\u1ecdn g\u00e0ng c\u1ee7a n\u00f3: s\u1eed d\u1ee5ng t\u00ean c\u1ee7a method.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">class ArticlesController &lt; ApplicationController\r\n\r\n\u00a0\u00a0before_filter :load_article\r\n\r\n\u00a0\u00a0# Actions...\r\n\r\n  private\r\n\r\n\u00a0\u00a0def load_article\r\n\r\n\u00a0\u00a0\u00a0\u00a0@article = Article.find(params[:id]) if params[:id]\r\n\r\n\u00a0\u00a0end\r\n\r\nend\r\n\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">C\u00e1c b\u1ea1n c\u00f3 th\u1ec3 \u0111\u1ec3 \u00fd th\u1ea5y m\u00ecnh \u0111\u1ec3 method <code>load_article<\/code> l\u00e0 private method. B\u1edfi v\u00ec filter n\u00e0y ch\u1ec9 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng b\u00ean trong controller, v\u00e0 s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c truy c\u1eadp b\u1edfi router \u0111\u1ec3 map v\u00e0o m\u1ed9t \u0111\u01b0\u1eddng d\u1eabn n\u00e0o, v\u00ec v\u1eady n\u00ean t\u1ed1t h\u01a1n h\u1ebft, ch\u00fang ta s\u1ebd \u0111\u1ec3 n\u00f3 l\u00e0 private method.<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<h3><span style=\"font-weight: 400;\"><code>after_filter<\/code>\u00a0<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Sau khi t\u00ecm hi\u1ec3u v\u1ec1 <code>before_filter<\/code> b\u00ean tr\u00ean, ch\u1eafc h\u1eb3n c\u00e1c b\u1ea1n c\u0169ng \u0111\u00e3 \u0111o\u00e1n ra <code>after_filter<\/code> s\u1eed d\u1ee5ng nh\u01b0 th\u1ebf n\u00e0o r\u1ed3i. N\u00f3 ho\u1ea1t \u0111\u1ed9ng v\u1edbi c\u01a1 ch\u1ebf t\u01b0\u01a1ng t\u1ef1 nh\u01b0 before_filter, ch\u1ec9 kh\u00e1c l\u00e0 \u0111\u01b0\u1ee3c th\u1ef1c thi sau khi action c\u1ee7a controller \u0111\u01b0\u1ee3c th\u1ef1c thi. B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng n\u00f3 \u0111\u1ec3 l\u1ecdc ho\u1eb7c mask c\u00e1c tr\u01b0\u1eddng th\u00f4ng tin nh\u1ea1y c\u1ea3m tr\u01b0\u1edbc khi tr\u1ea3 k\u1ebft qu\u1ea3 v\u1ec1 cho client ch\u1eb3ng h\u1ea1n.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\"><code>around_filter<\/code><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u00e2y l\u00e0 filter hi\u1ebfm khi \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng v\u00e0 c\u0169ng h\u01a1i kh\u00f3 hi\u1ec3u m\u1ed9t ch\u00fat x\u00edu. C\u00e1ch d\u00f9ng c\u1ee7a n\u00f3 nh\u01b0 sau:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">around_filter :wrap_actions\r\n\r\ndef wrap_actions\r\n\r\n\u00a0\u00a0begin\r\n\r\n\u00a0\u00a0\u00a0\u00a0# wrap action code will be here\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0yield # after yeild, action code will be run\r\n\r\n\u00a0\u00a0rescue\r\n\u00a0\u00a0\u00a0\u00a0render text: \"It broke!\" # if there is any exception throw by action code, reach here\r\n\u00a0\u00a0end\r\n\r\nend\r\n\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Nh\u01b0 m\u00ecnh \u0111\u00e3 comment trong \u0111o\u1ea1n code tr\u00ean, khi n\u00e0o <code>yield<\/code> \u0111\u01b0\u1ee3c g\u1ecdi, code b\u00ean trong action c\u1ee7a controller s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi. Trong qu\u00e1 tr\u00ecnh th\u1ef1c thi c\u1ee7a action, n\u1ebfu c\u00f3 exception x\u1ea3y ra, exception s\u1ebd \u0111\u01b0\u1ee3c b\u1eaft \u1edf \u0111o\u1ea1n code <code>rescue<\/code> c\u1ee7a <code>around_filter<\/code> b\u00ean tr\u00ean. \u0110o\u1ea1n code tr\u00ean l\u00e0 m\u1ed9t trong nh\u1eefng \u1ee9ng d\u1ee5ng c\u1ee7a around_filter: b\u1eaft exception.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Scope ph\u1ea1m vi c\u1ee7a filter: only v\u00e0 except<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">N\u1ebfu \u0111\u1ec3 m\u1eb7c \u0111\u1ecbnh, c\u00e1c \u0111o\u1ea1n code Filter \u1edf tr\u00ean s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi tr\u01b0\u1edbc t\u1ea5t c\u1ea3 request \u0111\u1ebfn b\u1ea5t k\u00ec action n\u00e0o b\u00ean trong controller. V\u1eady n\u1ebfu nh\u01b0 ch\u1ec9 mu\u1ed1n \u00e1p d\u1ee5ng \u0111o\u1ea1n code \u0111\u00f3 cho action <code>new<\/code> v\u00e0 <code>edit<\/code> th\u00f4i th\u00ec l\u00e0m th\u1ebf n\u00e0o? Sau \u0111\u00e2y m\u00ecnh s\u1ebd n\u00f3i v\u1ec1 c\u00e1ch scope ph\u1ea1m vi c\u1ee7a Filter t\u1edbi action m\u00e0 c\u00e1c b\u1ea1n mong mu\u1ed1n th\u00f4i: s\u1eed d\u1ee5ng <code>only<\/code> v\u00e0 <code>except<\/code>.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u1ea3 3 filter b\u00ean tr\u00ean \u0111\u1ec1u nh\u1eadn option <code>:only<\/code> v\u00e0 <code>:except<\/code><\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>:only<\/code>: danh s\u00e1ch c\u00e1c actions m\u00e0 filter \u0111\u00f3 s\u1ebd ch\u1ea1y c\u00f9ng<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>:except<\/code>: danh s\u00e1ch c\u00e1c actions m\u00e0 filter \u0111\u00f3 s\u1ebd kh\u00f4ng ch\u1ea1y c\u00f9ng<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5, \u1edf \u0111o\u1ea1n code v\u00ed d\u1ee5 cho <code>before_filter<\/code> b\u00ean tr\u00ean, c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 b\u1ecf \u0111i\u1ec1u ki\u1ec7n check <code>if params[:id]<\/code> n\u1ebfu nh\u01b0 b\u1ea1n scope filter n\u00e0y t\u1edbi c\u00e1c action s\u1ebd lu\u00f4n c\u00f3 <code>params[:id]<\/code>, v\u00ed d\u1ee5 nh\u01b0 <code>:show, :edit, :update, :destroy<\/code>.\u00a0 C\u00e1ch l\u00e0m nh\u01b0 sau:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">class ArticlesController &lt; ApplicationController\r\n\r\n\u00a0\u00a0before_filter :load_article, only: [:show, :edit, :update, :destroy]\r\n\r\n\u00a0\u00a0# Actions...\r\n\r\nprivate\r\n\r\n\u00a0\u00a0def load_article\r\n\r\n\u00a0\u00a0\u00a0\u00a0@article = Article.find(params[:id])\r\n\r\n\u00a0\u00a0end\r\n\r\nend\r\n\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Ho\u1eb7c c\u00e1c b\u1ea1n c\u0169ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <code>:except<\/code> nh\u01b0 sau:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">class ArticlesController &lt; ApplicationController\r\n\r\n\u00a0\u00a0before_filter :load_article, except: [:index, :new, :create]\r\n\r\n\u00a0\u00a0#...\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Nh\u01b0 v\u1eady l\u00e0 ch\u00fang ta \u0111\u00e3 \u0111i qua 3 lo\u1ea1i filter \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong Rails v\u00e0 c\u0169ng \u0111\u00e3 t\u00ecm hi\u1ec3u l\u00e0m sao \u0111\u1ec3 config c\u00e1c actions m\u00e0 ch\u00fang ta mu\u1ed1n ch\u1ea1y filter b\u00ean trong controller. Sau \u0111\u00e2y s\u1ebd t\u1edbi ph\u1ea7n th\u00fa v\u1ecb nh\u1ea5t, share c\u00e1c filter n\u00e0y gi\u1eefa c\u00e1c controller lu\u00f4n. B\u1eadt m\u00ed, ch\u00fang ta s\u1ebd s\u1eed d\u1ee5ng m\u1ed9t ch\u00fat <strong>metaprogramming<\/strong>.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Sharing Filters<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Th\u00f4ng th\u01b0\u1eddng, filter s\u1ebd \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 share code gi\u1eefa c\u00e1c action b\u00ean trong m\u1ed9t controller c\u1ee5 th\u1ec3. Tuy nhi\u00ean, ch\u00fang ta c\u00f2n c\u00f3 th\u1ec3 share ch\u00fang gi\u1eefa c\u00e1c controller, s\u1eed d\u1ee5ng <a href=\"https:\/\/www.marketenterprise.vn\/blog\/tinh-dong-goi-trong-lap-trinh-huong-doi-tuong.html\">OOP (l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng)<\/a>.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Share filter gi\u1eefa c\u00e1c controller th\u00f4ng qua <code>ApplicationController<\/code><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00e1ch th\u00f4ng th\u01b0\u1eddng nh\u1ea5t \u0111\u1ec3 t\u00e1i s\u1eed d\u1ee5ng filter gi\u1eefa c\u00e1c controller \u0111\u00f3 l\u00e0 di chuy\u1ec3n \u0111o\u1ea1n code filter n\u00e0y ra <code>ApplicationController<\/code>. B\u1edfi v\u00ec t\u1ea5t c\u1ea3 c\u00e1c controller kh\u00e1c c\u1ee7a ch\u00fang ta k\u1ebf th\u1eeba t\u1eeb <code>ApplicationController<\/code>, c\u00e1c controller n\u00e0y s\u1ebd c\u00f3 quy\u1ec1n truy c\u1eadp \u0111\u1ebfn c\u00e1c filter n\u00e0y.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">class ApplicationController &lt; ActionController::Base\r\n\r\n\u00a0\u00a0protect_from_forgery\r\n\r\n  private\r\n\r\n\u00a0\u00a0def load_article\r\n\r\n\u00a0\u00a0\u00a0\u00a0@article = Article.find(params[:id])\r\n\r\n\u00a0\u00a0end\r\n\r\nend\r\n\r\n\r\n\r\nclass ArticlesController &lt; ApplicationController\r\n\r\n\u00a0\u00a0before_filter :load_article, only: [:show, :edit, :update, :destroy]\r\n\r\n\u00a0\u00a0# Actions...\r\n\r\nend\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Nh\u01b0ng c\u00e1c b\u1ea1n c\u00f3 th\u1ea5y \u0111i\u1ec1u g\u00ec b\u1ea5t th\u01b0\u1eddng \u1edf trong \u0111o\u1ea1n code tr\u00ean hay kh\u00f4ng? \u0110\u00fang r\u1ed3i, ngo\u00e0i controller <code>ArticlesController<\/code> ra, c\u00e1c controller kh\u00e1c \u0111\u00e2u c\u1ea7n bi\u1ebfn <code>@article<\/code> l\u00e0m g\u00ec. V\u1eady c\u00f3 c\u00e1ch n\u00e0o \u0111\u1ec3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>ArticlesController<\/code> th\u00ec t\u1ea1o ra bi\u1ebfn <code>@article<\/code><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>PostsController<\/code> th\u00ec t\u1ea1o ra bi\u1ebfn <code>@post<\/code><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>OrdersController<\/code> th\u00ec t\u1ea1o ra bi\u1ebfn <code>@order<\/code><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u2026\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">hay kh\u00f4ng?<\/span><\/p>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng m\u1ed9t ch\u00fat metaprogramming, ch\u00fang ta c\u00f3 th\u1ec3 l\u00e0m \u0111\u01b0\u1ee3c. Ch\u00fang ta s\u1ebd s\u1eeda method <code>load_article<\/code> l\u1ea1i th\u00e0nh <code>find_resource<\/code>. Trong method n\u00e0y, s\u1eed d\u1ee5ng <code>params[:controller]<\/code> \u0111\u1ec3 suy ra model t\u01b0\u01a1ng \u1ee9ng, ki\u1ec3u nh\u01b0 sau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>ArticlesController<\/code> -&gt; Model: <code>Article<\/code> -&gt; bi\u1ebfn: <code>@article<\/code><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>PostsController<\/code> -&gt; Model: <code>Post<\/code> -&gt; bi\u1ebfn: <code>@post<\/code><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><code>OrdersController<\/code> -&gt; Model: <code>Order<\/code> -&gt; bi\u1ebfn: <code>@order<\/code><\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">class ApplicationController &lt; ActionController::Base\r\n\r\n\u00a0\u00a0protect_from_forgery\r\n\r\n  private\r\n\r\n\u00a0\u00a0def find_resource\r\n\r\n\u00a0\u00a0\u00a0\u00a0class_name = params[:controller].singularize\r\n\r\n\u00a0\u00a0\u00a0\u00a0klass = class_name.camelize.constantize\r\n\r\n\u00a0\u00a0\u00a0\u00a0self.instance_variable_set \"@\" + class_name, klass.find(params[:id])\r\n\r\n\u00a0\u00a0end\r\n\r\nend\r\n\r\n\r\nclass ArticlesController &lt; ApplicationController\r\n\r\n\u00a0\u00a0before_filter :find_resource, only: [:show, :edit, :update, :destroy]\r\n\r\n\u00a0\u00a0# Actions...\r\n\r\nend\r\n\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Nh\u01b0 v\u1eady qua b\u00e0i vi\u1ebft n\u00e0y, m\u00ecnh c\u00f9ng v\u1edbi c\u00e1c b\u1ea1n \u0111\u00e3 \u0111i qua c\u00e1c k\u1ef9 thu\u1eadt \u00e1p d\u1ee5ng Filter trong Rails \u0111\u1ec3 khi\u1ebfn code tr\u1edf n\u00ean ng\u1eafn g\u1ecdn v\u00e0 clean h\u01a1n. Hi v\u1ecdng b\u00e0i vi\u1ebft gi\u00fap \u00edch ph\u1ea7n n\u00e0o \u0111\u00f3 cho c\u00e1c b\u1ea1n tr\u00ean con \u0111\u01b0\u1eddng s\u1eed d\u1ee5ng framework Ruby on Rails trong c\u00f4ng vi\u1ec7c c\u1ee7a m\u00ecnh. C\u00e1c b\u1ea1n c\u00f3 chia s\u1ebb n\u00e0o th\u00eam c\u00f3 th\u1ec3 \u0111\u1ec3 d\u01b0\u1edbi ph\u1ea7n b\u00ecnh lu\u1eadn nh\u00e9.<\/span><\/p>\n<p>References:<\/p>\n<ul>\n<li><a href=\"http:\/\/tutorials.jumpstartlab.com\/\" target=\"_blank\" rel=\"noopener\">JumpstartLab<\/a><\/li>\n<li><a href=\"https:\/\/guides.rubyonrails.org\/action_controller_overview.html#filters\" target=\"_blank\" rel=\"noopener\">Ruby On Rails Guide about Filtering<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>C\u00e1c b\u1ea1n khi b\u1eaft \u0111\u1ea7u l\u00e0m vi\u1ec7c v\u1edbi Rails h\u1ea7u h\u1ebft \u0111\u1ec1u l\u00e0m vi\u1ec7c v\u1edbi 7 actions chu\u1ea9n khi t\u1ea1o m\u1ed9t Controller m\u1edbi: index, new, show, create, edit, update, destroy. Sau m\u1ed9t th\u1eddi gian l\u00e0m vi\u1ec7c, c\u00e1c b\u1ea1n s\u1ebd \u0111\u1ec3 \u00fd th\u1ea5y m\u1ed9t \u0111i\u1ec1u r\u1eb1ng, c\u00e1c b\u1ea1n th\u01b0\u1eddng s\u1ebd mu\u1ed1n d\u00f9ng \u0111i d\u00f9ng l\u1ea1i m\u1ed9t \u0111o\u1ea1n t\u00ednh n\u0103ng t\u01b0\u01a1ng t\u1ef1 nhau \u1edf c\u00e1c actions ho\u1eb7c th\u1eadm ch\u00ed l\u00e0 c\u00e1c controllers. Trong b\u00e0i vi\u1ebft n\u00e0y ch\u00fang ta s\u1ebd c\u00f9ng \u0111i chung v\u1edbi nhau tr\u00ean con \u0111\u01b0\u1eddng gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1 n\u00e0y v\u1edbi m\u1ed9t t\u00ednh n\u0103ng r\u1ea5t hay: Filters.<\/p>\n","protected":false},"author":28,"featured_media":2691,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[118],"class_list":["post-2689","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-rails"],"_links":{"self":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/2689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/comments?post=2689"}],"version-history":[{"count":0,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/2689\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media\/2691"}],"wp:attachment":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media?parent=2689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/categories?post=2689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/tags?post=2689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}