更改选择框更改时的布局

时间:2009-02-02 12:48:44

标签: ruby-on-rails prototype

我想根据选择框中的选择渲染部分。这看起来微不足道,但我找不到一个例子。

非常感谢所有帮助。

问候

2 个答案:

答案 0 :(得分:2)

如果您只是尝试在表单提交的页面上呈现部分内容,

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

所以,如果你有一个像

这样的选择框
<select name='the_partial'>
    <input value="partial1">Some Partial</input>
    <input value="partial2">Another Partial</input>
</select>

你需要渲染

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

假设您的视图文件夹中有_partial1.html.erb和partial2.html.erb

答案 1 :(得分:1)

我会从这样的事情开始:

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