Ruby/Rails - 是否存在此双重渲染错误(渲染+重定向)的解决方法

Chr*_*len 0 ruby-on-rails ruby-on-rails-3

我有一个问题,我认为我需要在同一个动作中使用重定向和渲染,我不断得到双重渲染错误,所以我想知道是否有人可以看到另一种方式.

我有一个带有选择框和按钮的表单,允许用户从一组城市中进行选择以进行导航.

<%= form_tag root_path, :method => :post do %>
  <%= select_tag :city, options_for_select(%w{ Pittsburgh Philadelphia Austin }) %>
  <%= submit_tag "Change City" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

在控制器中,我检查城市参数然后重定向到所需的页面.

def index
  if params[:city] == "Pittsburgh"
    redirect_to pittsburgh_path
  elsif params[:city] == "Philadelphia"
    redirect_to philadelphia_path
  elsif params[:city] == "Austin"
    redirect_to austin_path         
  end
  render :layout => false
end
Run Code Online (Sandbox Code Playgroud)

但是在这个页面上,我创建了一个特定的布局/设计,只在该页面上,所以我只关闭了应用程序布局.

我可以同时执行导航操作并关闭应用程序布局的渲染吗?

Sim*_*tti 7

是的,将支票移入before_filter.

class MyController < ApplicationController

  before_filter :redirect_customized_cities, :only => :index

  def index
    render :layout => false
  end

  protected

    def redirect_customized_cities
      case params[:city]
        when "Pittsburgh"
          redirect_to pittsburgh_path
        when "Philadelphia"
          redirect_to philadelphia_path
        when "Austin"
          redirect_to austin_path
      end
    end
end
Run Code Online (Sandbox Code Playgroud)