如何从控制器中的索引两个不同的按钮重定向两个不同的URL?

时间:2013-01-17 16:10:28

标签: ruby-on-rails ruby ruby-on-rails-3

如何使用控制器中的单个方法将两个链接重定向到两个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

2 个答案:

答案 0 :(得分: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) %>

答案 1 :(得分: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 "/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" %>