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
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.
in your routes
in your view