0

How to redirect the two links to two urls by using single method in controller?

Example:

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

in my view,

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

in my routes

resources :current_epc, only: [:index, :show] do
  get :download, :on => :collection
end
Sri
  • 2,233
  • 4
  • 31
  • 55
  • a "button" is a submit button? this has been asked lots of times, for exemple http://stackoverflow.com/questions/3027149/how-do-i-create-multiple-submit-buttons-for-the-same-form-in-rails – tokland Jan 17 '13 at 16:16
  • sorry, thats not the button, thats the link .. – Sri Jan 17 '13 at 16:17
  • @tokland, Yeah, i need the same of the posted question by you. But its not a button its a link. Thanks – Sri Jan 17 '13 at 16:18

2 Answers2

1

I would consider create 2 differente actions. One for each case. It would make your actions and your code much easier to read.

Then u would result with 3 actions. Index which would only load the initial objects, one to treat the specific id by the first logic and another link to treat the second logic.

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

in your routes

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

in your view

<%= link_to 'PDF', enr_location_current_epc_pdf_path(@location) %>
<%= link_to 'LIVE', enr_location_current_live_epc_path(@location) %>
Paulo Henrique
  • 1,025
  • 8
  • 12
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

In your routes.rb:

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

And your two links:

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

EDIT: member is used when it requires an :id parameter, if not it is a collection. Anyway, I would use match in that case like (which is in parenthesis is optional):

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

and get the parameter in your controller like this:

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

EDIT 2: Index controller:

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

Your routes:

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

Your view (change links to fit your tastes, it's just a simple example):

<%= link_to "first", "/anything/1/pdf" %>
<%= link_to "second", "/anything/1/live" %>
Louis XIV
  • 2,224
  • 13
  • 16
  • I updated my question... Please suggest me to do in the routes. – Sri Jan 17 '13 at 16:36
  • Don't get what's is unclear for you? Just add a param to your url and make a conditional render in the action with this param. Am I off the subject? – Louis XIV Jan 17 '13 at 16:43
  • Nope, I am unclear with the routes. I post my routes in the question. – Sri Jan 17 '13 at 16:45
  • hi sorry Louis.. I edited the question again. Please have a look – Sri Jan 17 '13 at 17:25