Please help... :) Working on this for a couple of days now
I have a submittal database with stored url's to manufacturer installation instructions generally in PDF formats. I don't want to use the default to_path because I'll have to store the documents locally on my server. Here's the snippet of code that I'm using to pull the url path from my database. As you can see below without the path_to rails wants to turn the url into a path (see error below). I tried adding quotes but then rails doesn't read the code correctly. I've read about helpers but haven't gotten them to work.
<%= #wf_room.wf_lights.pluck(:typemark).count %> <%= link_to #wf_room.wf_lights.map(&:typemark).uniq, #wf_room.wf_lights.map(&:url).uniq %>
error:
undefined method `http://www.sistemalux.com/en/files/ficheproduit/7050_Sliver_wall(13).pdf_path' for #<#:0x0000000404bad8>
Have you tried string interpolation?
<%= link_to #wf_room.wf_lights.map(&:typemark).uniq.first, "#{#wf_room.wf_lights.map(&:url).uniq.first}" %>
(I added first method because won't they the current methods return arrays?)
Related
I'm working on a Ruby on Rails project that uses Paperclip for file uploads, S3 for storage, and does some back-end image conversion using Blitline. The result of the conversion gives the original and a file called upload.png in my S3 bucket alongside the original.
So, after conversion I've got two files something along the lines of:
myaws.amazonaws.com/mybucket/model_id/original.pdf and
myaws.amazonaws.com/mybucket/model_id/upload.png
Ideally, I would like to keep the original at hand in my bucket case my user needs to download it again, or if we need to do another conversion for some reason.
Is there a method similar to <% = image_tag #attachment.url %> that will specify the file 'upload.png'?
Edit (More info:)
I did attempt <% = image_tag #attachment.url, :format => :png %> though it does not work. Seems as if rails is still trying to pull it up as a PDF
Have you specified a style for your attachment? If you have one, let's say xyz then you can get the url <% = image_tag #attachment.url(:xyz) %>
<div class="img-container">
<%= if #user.image_url.present? %>
<%= image_tag #user.image_url(:main).to_s %>
<%= end %>
</div>
I am using rails 4, and carrier wave to upload photos as mentioned in the rails casts. So I have a column in my db that is called "image", and the above code works without the if statement. When I use the #user.image_url(:main).to_s and it has an image it properly shows the image in the container. I want to upload a standard photo when the user does not provide one. It's located in my assets/images folder.
How can I get the if statement to detect if there is photo present or not in the column image? I have to use image_url if showing the user uploaded photo. Not just image to display the image, and the .to_s is a safety net. Any thoughts or answers?
Thanks!
Thanks to the accepted answer, I did use the suggested carrier wave solution for Rails 4 which is recommended for 3.1 and above. This post helped me get it corrected: Default URL not loading with Carrierwave in Rails
Specifying a default url with CarrierWave should do the trick. This allows for a fallback if no image is currently present.
Example:
def default_url
ActionController::Base.helpers.asset_path "fallback/main/default.jpg"
end
For Rails 5 the one that worked for me is
ActionController::Base.helpers.resolve_asset_path("logos/smthg.png")
returns nil if the asset is absent
and
the asset path if present
👍
When I run my initial migration I get a "new.html.erb" file for each of my models. Its a very simple looking form, but the "new.html.erb" contains just the following code..
<h1>New city</h1>
<%= render 'form' %>
<%= link_to 'Back', cities_path %>
Now I want to modify this page, but I dont really understand whats going on here, where in the project this 'form' is located?
The form is located under app/views/cities/_form.html.erb.
I strongly suggest you to read this page before start working with ruby - it will give you a better understanding of how things works.
Since Rais works on convention over configuration, you will have a bad time trying to figure out everything by yourself. However, once you are used to the conventions, your will have a very fast development speed.
In the same folder where your new view is located.
The release notes for Rails 3.0 include this change:
7.4.2 Helpers with Blocks
Helpers like form_for or div_for that insert content from a block use <%= now:
<%= form_for #post do |f| %>
...
<% end %>
Your own helpers of that kind are expected to return a string, rather than appending to the output buffer by hand.
Helpers that do something else, like cache or content_for, are not affected by this change, they need <% as before.
We're in the process of migrating a web application from Rails 2.3.18 to Rails 3.1.12, and it would be very useful to have a complete list of such helpers that have changed, so that we can check all of their occurrences in our source code, but I'm having trouble finding an authoritative list of this kind.
I've tried looking through the git history of the rails project, but there seem to be many commits with related changes, and they're not obviously grouped on particular branch. For example, it seems to be clear that this list includes:
form_for
form_tag
fields_for
field_set_tag
... from 7b622786f,
link_to
... alluded to in e98474096 and:
div_for
content_tag_for
... alluded to in e8d2f48cff
remote_form_for
.... alluded to in 0982db91f, although it's removed in Rails 3.
However, I'm sure that's not complete - can anyone supply a complete list?
i don't have a complete list, but i think that you can derive most of what has changed from having a look at the diff in documentation of UrlHelper and FormHelper. most of the methods in those helpers changed to the new syntax.
http://apidock.com/rails/v2.3.8/ActionView/Helpers/UrlHelper/link_to
http://apidock.com/rails/v2.3.8/ActionView/Helpers/FormHelper/form_for
There is a list of these methods in the rails_upgrade plugin, whose purpose is to check your application for problems on upgrading from Rails 2 to Rails 3. The relevant method is check_old_helpers, which checks for block helpers containing any of:
content_tag
javascript_tag
form_for
form_tag
fields_for
field_set_tag
As for how authoritative this is, this plugin is an official Rails project plugin, although it does miss out a couple that I found by searching the git history:
div_for
remote_form_for
link_to
However, if the official tool for checking for these helpers is missing some, perhaps this is as good a list as I'm likely to find. Another point is that the upgrade check tool mentions that there should be deprecation warnings if you miss some, which provides an additional check:
Block helpers that use concat (e.g., form_for) should use <%= instead of <%. The current form will continue to work for now, but you will get deprecation warnings since
this form will go away in the future.
I'm working on upgrading an old Rails app (1.1.6) to Rails 3. Obviously, a lot has changed. One thing appears to be that Rails automatically escapes content dropped into the view. However, I have a situation where I have a helper generating IMG tags for me, and Rails is automatically escaping the resulting content.
<%= random_image('public/images/headers') %>
This results in escaped content, much like one would expect had I done this (in 1.1.6)
<%= h random_image('public/images/headers') %>
Is there a way to tell it to not escape?
<%= raw random_image('public/images/headers') %>
.html_safe
It may need to be inside the helper
There are there ways in which this can be achieved in rails 3 application
html_safe
raw
h
raw and h can only be used in controller and views these methods are defined in helpers.
html_safe can be used anywhere in a rails application i.e., can be used in models, helpers, controller etc.
For more information please read http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/