Rails 7.1 allows templates to set strict locals
source link: https://blog.kiprosh.com/allow-template-to-set-strict-locals/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
rails_7 Published on 7 December 2022 7 December 2022 • 2 min read
Rails 7.1 allows templates to set strict locals
As our view files get larger, we make use of partial templates to move some code to its own file. This also helps us to follow the single-responsibility principle. Partials also accept local variables, enhancing their flexibility and power.
Before Rails 7.1
Prior to Rails 7.1, we could not restrict which local variables we can pass to the partials.
Rails 7.1 onwards
Rails 7.1 adds the ability to define the local variables a template can accept. This helps in precompiling templates during application startup rather than at runtime. To achieve this, we have to add a locals magic comment inside the partial.
# app/views/dashboard/_contact_us.html.erb
<%# locals: (address:, mobile:) -%>
<%= address %>
<%= mobile %>
With the help of the magic comment, we have defined only two locals address
& mobile
. This can be passed to the partial as follows:
# app/views/homepage/index.html.erb
<%= render partial: 'contact_us', locals: { address: 'Mumbai', mobile: '987654321' } %>
Now, if we try to pass an additional local variable, let's say company
# app/views/homepage/index.html.erb
<%= render partial: 'contact_us', locals: { address: 'Mumbai', mobile: '987654321', company: 'Test Company' } %>
The above will throw the following error in the server log
ActionView::Template::Error (unknown local: :company):
app/views/homepage/index.html.erb:2
Also, if we define a local variable but do not pass it while rendering the partial, for instance, let's fail to pass the defined local variable mobile
on rendering the partial
# app/views/homepage/index.html.erb
<%= render partial: 'contact_us', locals: { address: 'Mumbai' } %>
The server log will warn us with the following error
ActionView::Template::Error (missing local: :mobile):
app/views/homepage/index.html.erb:2
Passing default values
We can also pass a default value to the locals as follows:
# app/views/dashboard/_contact_us.html.erb
<%# locals: (address: 'Delhi', mobile: 1234567890) -%>
<%= address %>
<%= mobile %>
Now, if we missed out to pass the locals while rendering the template/partial, it will use the default value assigned by the magic comment.
Disabling locals
We can disable passing any locals to the partial. Add the following magic comment to the partial.
# app/views/dashboard/_contact_us.html.erb
<%# locals: () %>
<h1>Contact Us</h1>
<p>Mumbai</p>
<p>1234567890</p>
Now, if we try to pass locals while rendering the above partial, the server log will throw the below error.
ActionView::Template::Error (no locals accepted):
app/views/homepage/index.html.erb:2
References
Recommend
-
11
Rails 6.1 allows attribute's default to be configured but keeping its type Nov 15, 2020 , by Romil Mehta 1 minute read Rails allows us to change the type and set the default v...
-
10
Rails has added support for prepending concerns. Prepending If we prepend a module to a class, the module will be in...
-
13
Rails 6.1 has added a feature wherein we can specify an alias name of the model in the where clause. Let’s say we have an Employee model as below:...
-
6
Rails 7 allows constructors on has_one :through associations Apr 27, 2021 , by Mayank Khanna 1 minute read When we declare a...
-
8
Since Rails 5.2 was released, we can make use of the Credentials API to manage our secrets. Check out our previous blog on Rails cred...
-
16
We often come across cases in the Rails application where we need to bulk-insert records. Rails 6 introduced insert_all and upsert_all methods to solve bulk import issues.
-
5
How to Set Strict SafeSearch Results on the Web Search for Windows 11 By Neeraj Paruthi Published 21 hours ago...
-
5
Friday, August 5, 2022 This Week In Rails Strict Template Locals Detecting Unused Routes And An Improved Find_or_create_by 1366f9d7 layout: post ti...
-
3
Typescript strict mode plugin Typescript plugin that allows turning on strict mode in specific files or directories. Do I need this plugin? typescript-strict-plugin was created mainly for existing projects tha...
-
8
SQLite is usually very forgiving of unexpected or unusual inputs. However, when moving from SQLite to another database, this can cause problems. Rails 7.0 adds a :strict option to the default database.yml file to cir...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK