Linking to external file in Ruby on Rails
You should place the file in the app's public/
directory and use a forward slash at the start of the path in your link's href.
The problem you are having is because href="somefile.pdf"
is relative to the current URL which is probably something like http://localhost:3000/pages/42
. By using href="/somefile.pdf"
instead the resolved URL will be http://localhost:3000/somefile.pdf
(rather than http://localhost:3000/pages/somefile.pdf
) and it won't conflict with your pages routes.
create a download link with rails to an external file with at URL
what worked
def full_res_download
@asset = Asset.find(params[:id])
@file = open("http://86e.r54.cf1.rackcdn.com/uploads/fake/filepath.mov")
send_file( @file, :filename => File.basename(@asset.file.path.to_s))
end
real code
controler.rb
def web_video_download
@asset = Asset.find(params[:id])
@file = open(CDNURL + @asset.video_file.path.to_s)
send_file( @file, :filename => File.basename(@asset.video_file.path.to_s))
end
development.rb
CDNURL = "http://86e.r54.cf1.rackcdn.com/"
Adding external js files on Rails 6
Your folder needs to be structured like that:
app/javascript/packs/application.js
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("custom")
app/javascript/custom/index.js
require("myScript")
require("mySecondScript")
The myScript.js
and mySecondScript.js
files, for instance, will be located in the same folder ie app/javascript/custom
.
Rails external URL work as a link in views
If your form is inserted data properly then, You can work on defining a helper class for link like
#=> helpers/application_helper.rb
def fb_link(user_fb)
link_to "Facebook", "#{url_with_protocol(user_fb)}", class: "your-class", target: "new"
end
#=> url helper
def url_with_protocol(url)
/^http/.match(url) ? url : "http://#{url}"
end
Then from view
= fb_link(@user.fblink)
Or you can work like this
= link_to "Facebook", "#{url_with_protocol(@user.fblink)}", class: "your-class", target: "new"
Hope it helps
External link routes in rails
I would have kept external links only in views. Because this links are not related to the any kind of logic of the application, and it's just an UI elements.
So, this way seems to me the best:
<%= link_to 'Google', "http://google.ie" %>
If you need to use this element many times, maybe it makes sense to bring this code into the helper, for example:
def search_engine_link
link_to 'Google', "http://google.ie"
end
And I really think that is's not very good place to introduce a more complex logic.
Is it possible to include external routes files into the main routes.rb file?
(I've updated this answer to take advantage of the RouteReloader for development work)
You can easily accomplish this (even in Rails 4!).
config/routes.rb:
Rails.application.routes.draw do
resources :foo
end
config/routes/included.rb:
Rails.application.routes.draw do
resources :bar
end
config/initializers/routes.rb
Rails.application.routes_reloader.paths.unshift *Dir[File.expand_path("../../routes/**/*.rb", __FILE__)]
This will add all files under config/routes to the application routes, and it'll probably add them in reverse lexical order by filename. If you want to load the routes in a different order, rather than the glob, you can just push or unshift the routes onto routes_reloader.paths in the order desired.
rake routes:
Prefix Verb URI Pattern Controller#Action
foo_index GET /foo(.:format) foo#index
POST /foo(.:format) foo#create
new_foo GET /foo/new(.:format) foo#new
edit_foo GET /foo/:id/edit(.:format) foo#edit
foo GET /foo/:id(.:format) foo#show
PATCH /foo/:id(.:format) foo#update
PUT /foo/:id(.:format) foo#update
DELETE /foo/:id(.:format) foo#destroy
bar_index GET /bar(.:format) bar#index
POST /bar(.:format) bar#create
new_bar GET /bar/new(.:format) bar#new
edit_bar GET /bar/:id/edit(.:format) bar#edit
bar GET /bar/:id(.:format) bar#show
PATCH /bar/:id(.:format) bar#update
PUT /bar/:id(.:format) bar#update
DELETE /bar/:id(.:format) bar#destroy
Related Topics
Sending Form Data to Remote Rails Application Using Httparty
Regex to Extract Boundary and Content Type Out of Mail Headers
Use Some Middleware Only for Specific Rack Website
Rails Cors Issue with Ajax API Endpoint Request
Rails Console Is Adding Nil Instead of Values
Bug in Implemented Tagging System
Activerecord::Recordnotfound in Articlescontroller#Show Couldn't Find Article Without an Id
Rails + Google Calendar API Events Not Created
Proc Throws Error When Used with Do End
How to Use Link_To with Ajax in Rails 3.2.1
Rails: How to Use Scope to Find an Element in Array of Arrays
Define a Class Method in a Module
Save Google Cloud Speech API Operation(Job) Object to Retrieve Results Later
Sum of All Amount If Charge Dates Are the Same in Stripe
Make a Ruby Script with Text Io Double Clickable Executable File