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



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

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

那么你会得到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) %>
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

在你的路线上。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是可选的(:

 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" %>

最新更新