0

如何通过在控制器中使用单一方法将两个链接重定向到两个 url?

例子:

def index
  @location_id = Location.find(@location_id)
  @epc = Enr::Rds::CurrentEpc.find_by_location_id(@location_id)
  if # PDF EPC link clicked
    @epc.current_epc_path[-4..-1] == '.pdf'
    content = open(@epc.current_epc_path, "rb") {|io| io.read }
    send_data content, :filename => 'epc.pdf', :disposition => 'inline'
  end
  if # LIVE EPC link clicked
    @epc = Enr::Rds::XmlData.find_by_location_id(@location_id)
    redirect_to @epc.report_url
  end
end

在我看来,

<%= link_to 'PDF', enr_location_current_epc_index_path(@location) %>
<%= link_to 'LIVE', enr_location_current_epc_index_path(@location) %>

在我的路线中

resources :current_epc, only: [:index, :show] do
  get :download, :on => :collection
end
4

2 回答 2

1

我会考虑创建 2 个不同的操作。每个案例一个。这将使您的操作和代码更易于阅读。

然后你会得到3个动作。仅加载初始对象的索引,一个用于通过第一个逻辑处理特定 id,另一个链接用于处理第二个逻辑。

def index
  @location_id = Location.find(@location_id)
  @epc = Enr::Rds::CurrentEpc.find_by_location_id(@location_id)
end

pdf_epc
  @location_id = Location.find(@location_id)
  @epc = Enr::Rds::CurrentEpc.find_by_location_id(@location_id)
  @epc.current_epc_path[-4..-1] == '.pdf'
  content = open(@epc.current_epc_path, "rb") {|io| io.read }
  send_data content, :filename => 'epc.pdf', :disposition => 'inline'
end

def live_epc
  @epc = Enr::Rds::XmlData.find_by_location_id(@location_id)
  redirect_to @epc.report_url
end

在您的路线中

resources :current_epc, only: [:index, :show] do
  get :download, :on => :collection
end
get "/pdf_epc/:id" => "current_epc#pdf_epc", :as => enr_location_current_epc_pdf
get "/live_epc/:id" => "current_epc#live_epc", :as => enr_location_current_live_epc

在你看来

<%= link_to 'PDF', enr_location_current_epc_pdf_path(@location) %>
<%= link_to 'LIVE', enr_location_current_live_epc_path(@location) %>
于 2013-01-17T17:44:00.273 回答
0
def pdf_url
    if (params[:url] == 1)
        do something
    else
        do something else
    end
    @epc = Enr::Rds::CurrentEpc.find_by_location_id(@location_id)
    if @epc != nil
      @epc.current_epc_path[-4..-1] == '.pdf'
      content = open(@epc.current_epc_path, "rb") {|io| io.read }
      send_data content, :filename => 'epc.pdf', :disposition => 'inline'
    end
end

在您的 routes.rb 中:

match "/anything/pdf_url/:url" => "anything#pdf_url"

还有你的两个链接:

<%= link_to "first", "/anything/pdf_url/1" %>
<%= link_to "second", "/anything/pdf_url/2" %>

编辑:成员在需要 :id 参数时使用,如果不是,它是一个集合。无论如何,我会在这种情况下使用 match (括号中是可选的):

 match "/anything(/download/:url)" => "anything#index"

并像这样在控制器中获取参数:

def index
    if params[:url] == 1 # Or whatever you put in your link_to
        # redirect_to url 
    else
        # redirect_to url
    end
end

编辑 2:索引控制器:

def index

  if params[:id]
      @location_id = Location.find(params[:id])
      @epc = Enr::Rds::CurrentEpc.find_by_location_id(@location_id)
      if params[:url] == 'pdf'
        @epc.current_epc_path[-4..-1] == '.pdf'
        content = open(@epc.current_epc_path, "rb") {|io| io.read }
        send_data content, :filename => 'epc.pdf', :disposition => 'inline'
      elsif params[:url] == 'live'
        @epc = Enr::Rds::XmlData.find_by_location_id(@location_id)
        redirect_to @epc.report_url
      end
  else
    @locations = Location.all
    respond_to do |format|
      format.html  
      format.json  { render :json => @locations }
    end
  end
end

您的路线:

match "/anything(/:id(/:url))" => "anything#index"

您的观点(更改链接以适应您的口味,这只是一个简单的示例):

<%= link_to "first", "/anything/1/pdf" %>
<%= link_to "second", "/anything/1/live" %>
于 2013-01-17T16:26:24.630 回答