views:

169

answers:

2

Hi all,

I want to render a partial depending on a selection in selection box. This looks trivial but I can't find an example.

All help is greatly appreciated.

Regards

A: 

I'd start with something like this:

app/views/_layout_selector.html.erb:
<form action="<%= request.request_uri -%>">
  <select name='layout'>
    <input value="blue">Blue</input>
    <input value="pink">Pink</input>
    <input value="green">Green</input>
  </select>
</form>

app/views/layouts/blue.html.erb:
<html>
  ...
  <%= render :partial => '/layout_selector' %>
  ...
</html>

(/app/views/layouts/pink.html.erb and green.html.erb similar)

app/controllers/application.rb:
class ApplicationController < ActionController::Base
  DEFAULT_LAYOUT = 'blue'
  layout :pick_layout
  ...
  private
  def pick_layout
    params[:layout] || DEFAULT
  end
end
James A. Rosen
A: 

If you're simply trying to render a partial on the page that the form submits to,

<%= render :partial => params[:your_selectbox_value_matching_the_partial_you_want] %>

So if you had a select box like

<select name='the_partial'>
    <input value="partial1">Some Partial</input>
    <input value="partial2">Another Partial</input>
</select>

You'd need to render

<%= render :partial => params[:the_partial] %>

Assuming you have _partial1.html.erb and partial2.html.erb in your view folder

John Paul Ashenfelter