Fun with Variants and Enums in Rails 4.1
Variants can be used within a respond_to block to handle the same action in separate ways. Using the
Fun with Variants and Enums in Rails 4.1:
'via Blog this'
Variants
Variants can be used within a respond_to block to handle the same action in separate ways. Using the
User
model from above, we can render a show page unique for a particular user's role.def show
@user = User.find(params[:id])
request.variant = @user.role.to_sym
respond_to do |format|
format.html do |html|
html.student
html.staff
html.admin
end
end
end
In this example, the variants of the show action such as
html.student
will render a view template with the name matching: show.html+student.haml
(or erb if you prefer). This allows us to use the same logic from ourFun with Variants and Enums in Rails 4.1:
'via Blog this'