I have an angular file set up with my view template urls that gets used in my app when setting up routes. One of the urls will change based on the rails environment and I don't always know at least for the staging environment, what the url will be. So I was hoping to use rails request.host_with_port but when I run it I get the error
undefined local variable or method `request'
Here's the templates.js.erb file. Is there another way I can do this? Thanks.
angular.module('jobBoard')
.constant('templates', {
apply : '<%= asset_path('apply.html') %>',
detail : '<%= asset_path('detail.html') %>',
<% if Rails.env.development? || Rails.env.test? %>
apiUrl: 'http://localhost:3000/api'
<% elsif Rails.env.staging? %>
apiUrl: <%=request.host_with_port%>'/api'
<% else %>
apiUrl: 'http://www.knownurl.com/api'
<% end %>
});
Related
I am using ActionText with image upload using the image_processing gem. However, when uploading images they do not go to S3 which is set in Active Storage.
All other image uploads work fine and go into my S3 buckets as expected. It is just images uploaded into the ActionText editor that does not use the S3 URL. They do go into the correct buckets, however. It is just the URL that I can't change or get access to in the blob element.
I assume there is a setting in an initializer or config that I have not been able to find but I can't find it through the documentation or google.
The URL the blob is currently using is the rail/active_storage/ storage URL and I need it to use the S3 URL I have set in the active_storage.yml file.
Turns out I could edit the URL in the _blob.html.erb file. First I had to check if I was in Production or not. Once I verify that I was able to construct the URL to grab from S3 using the blob.key.
<% if blob.representable? %>
<% if Rails.env == 'production' %>
<img class: "post-actiontext-image" src="https://<%= ENV['S3_BUCKET'] %>.s3-<%= ENV['AWS_REGION'] %>.amazonaws.com/<%= blob.key %>" />
<% else %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]), class: "post-actiontext-image" %>
<% end %>
<% end %>
I'm using this tutorial to try to facilitate image uploads using the TinyMCE WYSIWYG editor on my rails 5.2 app.
So far I have implemented all the code in the tutorial and everything works perfectly, but when I try to upload an image I get a "Got a bad response from the server" error message.
In my heroku logs I then get this:
FATAL -- : [527c4468-9ebe-475a-97a9-380bfc327aab] ActionController::RoutingError (uninitialized constant #<Class:0x000055b6d991e020>::EditController
2020-02-22T17:34:07.814727+00:00 app[web.1]: Did you mean? DeviseController):
Here is the route I'm using:
post '/tinymce_assets', to: 'article/edit#image_upload'
With these controller methods:
def image_upload
file = params[:file]
url = upload_file(file)
render json: {
image: {
url: url
}
}, content_type: "text/html"
end
private
def upload_file(file)
s3 = Aws::S3::Resource.new(region:ENV['AWS_REGION'])
obj = s3.bucket(ENV['S3_BUCKET_NAME']).object('articles/images/content/' + filename(file))
obj.upload_file(file.tempfile, {acl: 'public-read'})
obj.public_url.to_s
end
def filename(file)
file.original_filename.gsub(/[^a-zA-Z0-9_\.]/, '_')
end
And this to initialize it:
<%= f.text_area :body, class: "tinymce", rows: 20, cols: 120 %>
<%= tinymce :content_css => asset_path('application.css')%>
...
<script>
$(document).ready(function() {
tinymce.init({
selector: "textarea.tinymce", // change this value according to your HTML
});
});
</script>
Can anyone see where I'm going wrong here?
Rails is trying to find an EditController because of your route: post '/tinymce_assets', to: 'article/edit#image_upload'
This route suggests that the controller it should look in is Article::EditController. Since you are actually looking for the ArticlesController you should change your route to be:
post '/tinymce_assets', to: 'articles#image_upload'
The Rails routing documentation can be helpful, specifically section 2.2 and 2.6. https://guides.rubyonrails.org/routing.html
I added the following in my production.rb environment file.
config.action_mailer.default_url_options = {host: "domain.com"}
In my view:
<%= tag("img", src: image_url("logo.png")) %>
However, when I look at the path in my email, I see a image_path, not URL.
http:///assets/logo.png
What am I doing wrong?
This is how it works in my application. Try this
<%= image_tag(attachments['logo.png'].url, style: 'margin: 5px') %>
Try asset_path instead of image_url in production email views.
You need to specify config.action_mailer.asset_host = "domain.com" in production.rb. Then use image_tag in your mailer view.
For <%= image_path('star-half-big.png') %>
I am seeing: http://localhost:3000/assets//assets/star-half-big.png
I have confirmed that the image is available at /assets/star-half-big.png
Any idea why rails is generating paths like this, and how to fix?
Make sure you put the image under app/assets/images/ directory.
And, then try it using image_url:
<%= image_url('star-half-big.png') %>
Also, make sure you don't have config.assets.prefix = "/assets/" in your config file.
It could be that in your development.rb you have an asset_host config set.
Comment it out if so.
# config.action_controller.asset_host = "localhost:3000/assets/"
I've realized my mistake. I had for path: '/assets/' set in $.fn.raty.defaults = {} inside of raty.js
I now have path: '../' in $.fn.raty.defaults = {} and in my views:
starHalf : '<%= image_path('star-half-big.png') %>',
starOff : '<%= image_path('star-off-big.png') %>',
starOn : '<%= image_path('star-on-big.png') %>'
I'm using S3 to serve my public folder & trying to build a simple URL to one of these assets.
My production.rb has:
config.action_controller.asset_host = "https://my-bucket.s3.amazonaws.com"
And this works perfect in my .erb files:
<%= image_tag("rails.png") %>
# => <img src="https://my-bucket.s3.amazonaws.com/rails.png" />
But I need a url (not a tag) for a GENERIC file type, like:
<%= asset_host "foo.bar" %>
# => https://my-bucket.s3.amazonaws.com/foo.bar
What is the magic, two-word, underscore joined, rails phrase that gives me this url?
Use <%= asset_path "foo.bar" %>.