Reputation: 14504
How to create different view modes. Like a visitor can choose list or box view.
Upvotes: 1
Views: 195
Reputation: 25991
I almost agree with ecoologic. Instead of second solution i would use something like case statement in controller which decides what template should be rendered.
How to create the helper method ?
Add it into <controller_name>_helper
file
Upvotes: 1
Reputation: 10420
It depends how much these views are similar, what I would do is to create partials for each view and then in the main view I call the right one depending on the property. Say you have a function in application_helper
which_view
in your index
you can write something like this:
<!-- your index -->
<%= render which_view == :box ? 'index_box' : 'Index_list' %>
I prefer the previous, but if you have few changes you can go for something like:
<% if session[:view_type] == 'box' %>
<!-- box content -->
<% else %>
<!-- list content -->
<% end %>
** EDIT **
make the action:
# application_controller.rb
def set_view_type # TODO: refactor
session[:view_type] = params[:view_type]
redirect_to :back
end
set your routes:
# routes.rb
match '/set_view_type' => 'application#set_view_type', :as => :set_view_type
write your form:
<!-- _view_type_selection.html.erb -->
<%= form_tag set_view_type_path do %>
<%= radio_button_tag :view_type, :box, session[:view_type] == 'box' %>
<%= radio_button_tag :view_type, :list, session[:view_type] == 'list' %>
<%= submit_tag 'select' %>
<% end %>
Not best practice but it works!
Upvotes: 1