Lets use the PeopleController#show action as an example. What if I need to display a person several different ways depending on the application. I have a few options based on the request format (js, html) but I really need more.
I could make different controller actions but I prefer to keep my controllers basic unless I have no sensible alternative. Another idea would be to have a case statement and a template parameter, but that seems to be bothersome as well (particularly if certain views are for certain user types).
My question is: what is the rails way to do this?
people_controller.rb
def show
set_partial
respond_to do |format|
format.js
end
end
protected
def set_partial
@template = case params[:template]
when 'small' then 'small_view'
when 'quick' then 'quick_view'
else
'full_view'
end
end
people/show.js.erb
$('body').append("<%= escape_javascript(render partial: @partial) %>")
Hi friend this might help you.