I am new to rails so forgive me if this is a basic question and I'm missing something.
I have a model that stores the location, filename, and extension of an image file. I concat those together and write them into an tag in my model using the image_tag. The tag is being correctly rendered in HTML with the proper path; however the image is showing up blank. I've added the image to app/assets/images so everything should be in its proper place but I can't figure out why the image isn't rendering. Here's what I have:
Model
class FileInfo < ActiveRecord::Base
def fullPath
"#{location}#{fileName}#{extension}"
end
end
View
<% #letter.relatedMedia.each do |m| %>
<%= image_tag(m.file_info.fullPath) %>
<% end %>
when the image tag renders in HTML, I get this as the source:
"/images/letters/some-image-file.jpg"
In my application, the file is stored here:
"app/assets/images/letters/some-image-file.jpg"
Am I storing the file in the wrong place? Is the path not writing out correctly? I'm really not sure where I'm going wrong here.
Any help you can provide would be greatly appreciated!
Change the path stored in the database to letters/some-image-file.jpg. You don't need the full path because the Rails asset pipeline will find the image for you.
Read about the Rails Asset Pipeline to get a better understanding about how it works.
Related
I have stored html code in a database field which is passed to the rails app.
<%= raw #exercise.explanation %>
The normal text is rendered correctly, it´s in the <p> .. </p>; but inside there the image codes are just displayed so output on the page is:
This is correct rendered text.
<%= image_tag ("exercises/picture.png"), style: 'height:auto; width:50%;' %>
Further text.
When I use <img src="exercises/picture.png"> or <img src="picture.png">inside the database entry, no picture is loaded either, just the broken image symbol of the browser.
Do you have the Assets Pipeline on? If you do, this could happen because of the generated hash that it put on your static assets to avoid cache problems. The thing is that the image_tag helper resolves this. It's a trade-off, but you could disable the Assets Pipeline and see if it works. To keep it on and do this, you will need a more elaborated solution.
You can try if it is that by disabling the assets digesting:
config.assets.digest = false
Alternatively you can have ERB interpret #exercise.explanation
<%= ERB.new(#exercise.explanation).result(binding) %>
This will take your #exercise.explanation and process it through ERB which appears to be what you are hoping for.
Caveat:
Be very careful what you allow to be stored in these "template" fields as things can go bad if you do not/can not sanitize this input. ERB#result is essentially a call to eval (which can be very dangerous)
I'm quite new to ruby and I'm trying to put the form to add a new post to the "application.html.erb" page.
Somehow I can't get it to work by just copying the code from the "app/views/things/_form.html.erb" page, and also I can't seem to get it to work by copying the code from the "app/views/things/new.html.erb" page.
If I do that I get the error:
First argument in form cannot contain nil or be empty
The error points towards:
<%= form_for(#thing) do |f| %>
This is a code that I find in the "app/views/things/_form.html.erb" page.
Can anybody point me in the right direction? To be honest, I don't even know where to start looking for the answer.
I think you should take a look at ruby on rails MVC model first, and also go through a basic tutorial. Just a quick review of you question:
application.html.erb is usually in the layout folder of rails,
it's job is to provide a root template/layout for the whole project if not specified.
usually you don't want to put any code that is not commonly
needed by all pages.
the reason it is giving you the error First argument in form cannot contain nil or be empty is because, the helper method form_for is expecting the controller to provide a #thing to the view file application.html.erb, in your case, it is obvious that #thing is not exist in your controller or your ApplicationController.
The #thing should be initialized in your controller. I suppose you've got a things_controller.rb that has a new action in it. In this action, controller should initialize #thing and therefore it is visible in app/views/things/new.html.erb.
You really shouldn't put forms in application.html.erb, but just to make the error disappear, you could do
<%= form_for(Thing.new) do |f| %>
(assuming you've got a Thing model in app/models/thing.rb).
This way you'll initialize an instance of Thing right in your application.html.erb view.
Article in my app has field "body", which contains text with html. For example:
<div class ='skill_pic'>
<img src = "/assets/images/picture_1.png">
</div>
In view I call field "body" that:
<%= raw #article.body %>
In local machine all is ok, but when I deploy my app on production, images don't display right and browser console returns a 404 error.
image_tag in article body can solve my problem, I mean:
<div class ='skill_pic'>
<%= image_tag('images/picture_1.png') %>
</div>
But helper raw can't convert text with html and rails method
Now, I think in three directions:
find right path for images on production
find some helper, which can right convert text with html and rails code
find another solution
I suppose you want to do some kind of mini-CMS. Be careful not letting users fill in data in your database. You get really bad security problems. If user can edit your html, you better use some kind of templating engine like Liquid or markdown.
That being said, you should just not use the asset pipeline for the referenced images. put the images in the public folder instead of the assets folder. The public folder will be available like a web root '/', so without the 'public' in the url.
You could store erb partials like this in your database:
<div class ='skill_pic'>
<%= image_tag('images/picture_1.png') %>
</div>
And render them with ERB in your views:
<%= raw ERB.new(#article.body).result(binding) %>
I'm building a Rails app, but I'm using a plugin in which I have to render my images using only html.
Since I haven't deployed yet, all my images are in RAILS_ROOT/app/assets/images/, so to render an image I have to write the following code:
<img src="/assets/image.jpg">
But when I'm ready to deploy to the web and I perform a precompile, all my images are supposedly going to be moved to my public folder. Will the html still work to link to the image, or will I have to change to link to a different path?
The plugin I'm using is Typeahead:
application.html.erb*
<script type="text/javascript">
//....
$('#typeahead').typeahead(null, {
maxLength: 5,
displayKey: function(thing) {
return "<div class='typeahead'><img src='" + thing.image_url + "'></div>";
},
source: bloodhound.ttAdapter(),
});
</script>
things_controller.rb
def typeahead
#render json: Thing.where(name: params[:query])
q = params[:query]
render json: Thing.where('name LIKE ?', "%#{q}%")
end
*Thing.image_tag is currently set to "/assets/[image.jpg]", except for each thing it's adjusted with the proper file name.
Not only are they going to be in the public folder, but they'll be renamed to include the fingerprint.
You must use the Rails helpers for all assets, see how to here and read the rest of the guide while you're at it :)
I think you should use non-stupid-digest-assets gem as it copies all your assets(mentioned in assets precompile list) in public/assets folder and then you need not to change your code before/after compiling.To install, you just need to add it into your Gemfile.
gem 'non-stupid-digest-assets'
I hope it might help you.
Joe, my suggestion would be to create a directory in your public folder to house your images, instead us using the app/assets directory. The public folder will allow the assets to not be altered by the rails pipeline, and you can link to them reliably using any external services that need the images.
As stated in RailsGuides:
Assets can still be placed in the public hierarchy. Any assets under
public will be served as static files by the application or web server
when config.serve_static_files is set to true. You should use
app/assets for files that must undergo some pre-processing before they
are served.
So you would need to add this line in config/application.rb
config.serve_static_files = true
As described in Rails general configuration.
It looks like you're storing your image_url in your model, and that's not working because assets don't have fixed URLs in Rails. I would override the getter in your model to use the asset_path helper, so it translates the path when that attribute is read (e.g., when the JSON is generated).
Something like:
# thing.rb
[...]
def image_url
ActionController::Base.helpers.asset_path(read_attribute(:image_url))
end
[...]
Short answer, no.
But it isn't that big a deal to remedy. Just move the images you need to reference with html into your Public folder. Then you can simply reference them with this code:
<img src="/image_name.image_type">
and the html will link to the correct path, both before and after precompile. So you don't have to change any code before you deploy.
BTW: I assume image_tag and image_url are the same column and you just made a mistake in one of the two times you mentioned it. If that's the case, then don't forget to change it to simply "/[image.jpg]".
I have a little problem with an output in ruby and rails.
I am still a beginner at rails, so it can be that the solution is pretty easy and i just can't see it.
I am trying to parse a website and put out some of the sourcecode on my own website.
Problem: it always puts out the whole source code as a text and is not interpreting the html code. how can i change that?
code:
page = Nokogiri::HTML(open("https://www.google.ch/search?client=ubuntu&channel=fs&q=google&ie=utf-8&oe=utf-8&redir_esc=&ei=WMpyUfSWEuz07AaTioCwDw"))
source = page.css('div#_css0')
<%= page %>
result:
http://postimg.org/image/dsaib9lx3/
I want to it to look like:
http://postimg.org/image/z9qlhoef5/
Thanks for any suggestions!
You should use raw in erb. It is actually equivalent to calling html_safe on it, but, just like h, is declared on a helper, so it can only be used on controllers and views.
page = Nokogiri::HTML(open("https://www.google.ch"))
<%= raw page %>
or
<%= raw Nokogiri::HTML(open("http://www.google.com")) %>
In Rails views every string content gets escaped, unless you use html_safe (docs)