Need Ruby on Rails developers for your project?
Contact Us

How to use descent exposure in Ruby on Rails | Inkoop Blog

How to use descent exposure in Ruby on Rails. We talk about its features, benefits and when and how to use the gem.

Posted by Ameena on 22 Apr 2018
How to use descent exposure in Ruby on Rails | Inkoop Blog

Although Decent exposure is a simple gem, it has different concept.

When you do not want to use instance variables across controller and view. You can definitely look into this gem.

In its term, it is used to expose a model in the controller.


#products_controller.rb
class ProductsController < ApplicationController
  expose :product
end

The gem provides a method called expose, which will either find the object with the help of params. If not present it builds the object. It performs either of the below query


Product.find(params[:product_id] || params[:id])

#or

Product.build(params[:product])

Now, you can use product instead of instance variable across the products controller and view.

It also handles custom behaviour if you pass in blocks,


#products_controller.rb
class ProductsController < ApplicationController
  expose(:product)
  expose(:products) { Product.all }
end

Likewise, you can work with associations


#reviews_controller.rb
class ReviewsController < ApplicationController
  expose(:product)
  expose(:reviews) { product.reviews }
  expose(:review)
end

Keep Coding !!!

Ameena


Need Ruby on Rails developers for your project?
Contact Us

Related Services.



Hire ReactJS Developers
Hire Gatsby Developers
Hire NextJS Developers

We support the Open Source community.



Have a Project in mind?