How to check if variable is empty in rails view? - ruby-on-rails

I am using paperclip in my rails app. I want to use an image uploaded via paperclip if an attribute appears for the field and use another image if it doesn't. With this code, the first portion of the if statement always executes regardless of whether there is an entry in the logo field. Any advice?
<% if #product.logo.url %>
<%= image_tag #product.logo.url, :style => 'width:60px;' %>
<% else %>
<img src = '/assets/logo/<%= #product.slug.downcase %>-sq.jpg' class="img-circle" style = 'width:60px;'>
<% end %>

You probably want to try calling String#blank?:
<% if #project.logo.url.blank? %>
<%= image_tag #product.logo.url, :style => 'width:60px;' %>
<% else %>
<img src = '/assets/logo/<%= #product.slug.downcase %>-sq.jpg' class="img-circle" style = 'width:60px;'>
<% end %>

Related

Strange output from rails each do

Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>

Shows "#" instead of text

I am executing this code:
def find_all_from_id
Note.find_by_sql([%{SELECT NOTE_N FROM NOTES WHERE ORDSP_ID = #{#order.order_number}}])
end
And in ERB file, I have an output:
<div class="form-field notes-div" >
<%= service_form.label :notes, "Pakalpojuma papildinformācija:", :class => "label_for_cod", :style=>"width: 170px;" %>
<div style="float:left; width:400px;"><%#= notes %> <%= find_all_from_id %></div>
</div>
It puts the # symbol instead of text the that comes from the database (http://prntscr.com/kop3mz). Why? And how can I fix that?
Take a look at the documentation for find_by_sql: https://api.rubyonrails.org/classes/ActiveRecord/Querying.html
This method will return an array of instances. If you want to display information about those instances you need to iterate through the array and manually display the information you want to show. Erb will not display the array of instances, leading to your issue.
E.g.
<% find_all_from_id.each do |note| %>
<%= note.name %>
<%= note.id %>
<% end %>

Ruby on Rails: How to print contents of variable in view and make checkbox?

I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>

Paperclip displaying multiple 'missing image' tags on edit page

So I have an object that has multiple photos (through use of a model called asset). I'm using paperclip to upload the photos and it's working fine. On my show page I call
<li class="grid_3">Photos:<br /></li>
<% #address.assets.each do |asset| %>
<li class='grid_3'><%= image_tag(asset.photo.url, size: '100x100') %></li>
<% end %>
and the photos display perfectly. when I click on the edit page:
<%= f.simple_fields_for :assets do |asset| %>
<%= render :partial => 'asset_fields', :locals => { :f => asset } %>
<% end %>
the partial is rendered:
<div class='nested-fields'>
<%= f.file_field :photo %>
<% unless f.object.new_record? %>
<%= f.file_field :photo %>
<% #address.assets.each do |asset| %>
<li class='grid_3'><%= image_tag(asset.photo.url, size: '100x100') %></li>
<% end %>
<% end %>
</div>
at this point the photos each show up 3 times along with an extra image tag with a 'missing' photo placeholder. I would say I've tried different things but I just don't see what is wrong with the code. Any ideas?
Assuming simple_fields_for behaves the same as fields_for... the call to f.simple_fields_for :assets is a loop already. It loops over f.object.assets and renders the contents of the block once for each item found using the asset form generator (it may be a little less confusing to call that block variable asset_form, by the way). So since you're then rendering a partial that also renders all of the assets for the #address that's why you get each image 3 times.

How to add Spree email attachment (image) from products.images?

I want to attach images to email from products Spreecommerce. But following code doestn't work.
../app/views/order_mailer/confirm_email.html.erb
<% for item in #order.line_items %>
# I'm not sure this product.image containts images. And it doesn't work =(
<%= image_tag image.attachment.url(:product), :itemprop => "image" %>
<%=item.variant.sku %> <%=item.variant.product.name%>
<%= variant_options(item.variant) %>(<%=item.quantity%>) # <%= number_to_currency item.price %>
<%= number_to_currency(item.price * item.quantity) %>
<% end %>
<%= number_to_currency #order.total %>
More understandable on the following picture.
Can you ask a solution to add image.products to confirmation email?
You need to do something like:
<%= image_tag "#{root_url}#{image.attachment.url(:product)}", :itemprop => "image" %>
You're getting a relative image path, not an absolute url.
./app/views/order_mailer/confirm_email.html.erb
<%= link_to image_tag('http://mysite.com' + item.variant.images.first.attachment.url(:small)), item.variant.product %>
Add this line in item block and you'll get image in confirmation email.

Resources