Develop the right mindset for Rails security
Avoid shipping vulnerable code by learning how to prevent security issues in your Rails applications.
Get the course for $99# app/views/products/index.html.erb
<% cache(product) do %>
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %># app/helpers/products_helper.rb
module ProductsHelper
def cache_key_for_products(products)
ids = products.pluck(:id).join('-')
max_updated_at = products.pluck(:updated_at).max
"products/#{ids}-#{max_updated_at.to_i}"
end
end# app/views/products/index.html.erb
<%= cache(cache_key_for_products(@products)) do %>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<% cache(product) do %>
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<% end %># app/helpers/products_helper.rb
module ProductsHelper
def cache_key_for_products(products, suffix = '')
ids = products.pluck(:id).join('-')
max_updated_at = products.pluck(:updated_at).max
"products/#{ids}-#{max_updated_at.to_i}#{suffix}"
end
endcache_key_for_products(@products, "jewel=#{@search.jewel_eq}")# app/helpers/products_helper.rb
module ProductsHelper
def cache_key_for_products(products, suffix = '')
ids = products.map(&:id).join('-')
max_updated_at = products.map(&id).max
"products/#{ids}-#{max_updated_at.to_i}#{suffix}"
end
end