0

コントローラーで単一のメソッドを使用して、2 つのリンクを 2 つの 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つの異なるアクションを作成することを検討します。ケースごとに 1 つ。これにより、アクションとコードが読みやすくなります。

次に、3つのアクションで結果が得られます。最初のオブジェクトのみをロードするインデックス。1 つは最初のロジックで特定の ID を処理し、もう 1 つは 2 番目のロジックを処理するリンクです。

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"

そしてあなたの2つのリンク:

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

EDIT: member は、: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 に答える