Question

Je souhaite rendre un partiel en fonction d'une sélection dans la zone de sélection. Cela semble banal mais je ne trouve pas d’exemple.

Toute aide est grandement appréciée.

Cordialement

Était-ce utile?

La solution

Si vous essayez simplement de générer un partiel sur la page à laquelle le formulaire est soumis,

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

Donc, si vous aviez une case de sélection comme

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

Vous devez rendre

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

En supposant que vous ayez _partial1.html.erb et partial2.html.erb dans votre dossier d'affichage

Autres conseils

Je commencerais par quelque chose comme ceci:

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top