How to use descent exposure in Ruby on Rails

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



What do we do?


We work on website development using Ruby on Rails, NodeJS, ReactJS, AngularJS. View all the services we provide here.


Say Hello! See our work