Am using bxslider in my rails app it works fine. It shows one image per slide but i want is to show two images per slide.
<ul class="bxslider">
<% #highlights.each do |highlight| %>
<% if highlight.area == current_or_guest_user.search_area or highlight.state == current_or_guest_user.search_state or highlight.city == current_or_guest_user.search_city %>
<li><%= image_tag highlight.image, :class => "dali" %></li>
<% end %>
<% end %>
</ul>
if i replicate the image_tag it does show two images in one slide but the same image.
You can get the records in pairs. Try this:
<ul class="bxslider">
<% #highlights.each_cons(2) do |highlight1, highlight2| %>
<% if condition %>
<li class="row">
<%= image_tag highlight1.image, :class => "dali col-xs-6" %>
<%= image_tag highlight2.image, :class => "dali col-xs-6" %>
</li>
<% end %>
<% end %>
</ul>
Related
This code grabs information from yaml files and outputs them in ascending order (I'm assuming by default). I can't figure out how to code this to reverse the order. Any suggestions? The site is using Middleman and ERB as templating language.
View:
<ul class="app-list">
<% data.apps.each do |app| %>
<% if app[1][:categories][:featured] && app[1][:categories][:script] %>
<li>
<%= partial 'partials/app', locals: { app: app[1] } %>
</li>
<% end %>
<% end %>
</ul>
Try:
<ul class="app-list">
<% data.apps.reverse.each do |app| %>
<% if app[1][:categories][:featured] && app[1][:categories][:script] %>
<li>
<%= partial 'partials/app', locals: { app: app[1] } %>
</li>
<% end %>
<% end %>
</ul>
I'm working on a pinterest-like app, using Cloudinary gem with Attachinary. I have added a line into my index.html.erb file which was supposed to display a default image if a created Pin didn't have one or the image didn't load properly.
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% if pin.image.present? == false %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>
Unfortunately it doesn't work - the images for the pins having them display properly, but for ones who don't only a thick line is visible (jQuery Masonry grid). I'm wondering what is the proper way of setting a default image with cloudinary. Where did I go wrong? Here is my full index file:
<div id="pins" class="transitions-enabled">
<% #pins.each do |pin| %>
<div class="container">
<div class="box panel panel-default">
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% if pin.image.present? == false %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>
<div class="panel-body">
<%= pin.description %> <br>
<strong><%= pin.user.email if pin.user %></strong>
<% if pin.user == current_user && user_signed_in? %>
<div class="actions">
<%= link_to edit_pin_path(pin) do %>
<span class="glyphicon glyphicon-edit black"></span>
Edit
<% end %>
<%= link_to pin, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-trash black"></span>
Delete
</div>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
I will only add that everything worked fine until I've introduced the glyphicons.
I will be greatful for your help!
If there was no copy&paste error, there is an extra end missing to terminate the link_to pin do statement. So the first if statement is still active, and then an opposite if statement is queried, which of course will never be true.
I suggest to use if-else-end.
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% else %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>
I've written the code in my .html.erb file but am thinking it might more sense to write it in my controller? This code returns all the names. I'm trying to figure out how to fetch a tag name only once and if a user inputs a tag name that has already been used it does not show up again.
<ul>
<% #pictures.each do |pic| %>
<% pic.tags.each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
<% end %>
</ul>
<ul>
<% #pictures.collect{|x| x.tags.uniq}.flatten.uniq.each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
</ul>
Using Enumerable#flat_map and Array#uniq:
<ul>
<% #pictures.flat_map { |pic| pic.tags.map(&:name) }.uniq.each do |name| %>
<li>
<%= name %>
</li>
<% end %>
</ul>
Example
<ul>
<% #pictures.each do |pic| %>
<% pic.tags.uniq_by(&:name).each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
<% end %>
Use the Array#- method for all items collected that the user enters.
this is relative to how you store the tag values, inside your save method you should make the tags array to be uniq.
Basically my controller is just grabbing all members: #members = Member.all and Im looping through them while checking to see if they have a profile picture uploaded and if not then the default should be loaded:
<% #members.each do |member| %>
<% unless member.image.nil? %>
<li style="float:left; width:100px;">
<%= image_tag(member.image.url(:tiny)) %>
<%= link_to member.email, member_path(member) %>
</li>
<% else %>
<li style="float:left; width:100px;">
<%= image_tag("default_member_small.jpg") %>
<%= link_to member.email, member_path(member) %>
</li>
<% end %>
<% end %>
It seems to think every member has a profile image, and the image tag is calling "images/tiny/missing.png" for the missing images.
What gives?
I am guessing you are using paperclip, if you are, you should not use nil?, you should use present?:
<% #members.each do |member| %>
<% if member.image.present? %>
<li style="float:left; width:100px;">
<%= image_tag(member.image.url(:tiny)) %>
<%= link_to member.email, member_path(member) %>
</li>
<% else %>
<li style="float:left; width:100px;">
<%= image_tag("default_member_small.jpg") %>
<%= link_to member.email, member_path(member) %>
</li>
<% end %>
<% end %>
And instead of having an if you should just have this image named as paperclip expects it, there should not have any ifs in your code for this kind of handling.
I am trying to make a simpel treelistmenu with ancestry.
Here is my code in view and it is not working:
<ul>
<% for cat_opg in CatOpg.roots %>
<li> <%= cat_opg.navn %><li>
<% for cat_opg in CatOpg.children %>
<li> <%= cat_opg.navn %><li>
</ul>
<% end %>
</ul>
<% end %>
And my controller:
def liste
#cat_opg = CatOpg.find(:all)
end
I want to make this simpel tree menu as:
Root
-children
Root
-children
I dont know what i am doing wrong.
PS: I am a rails beginner
First of all, you are getting to model in view, not to local variable.
Second, you're rewriting variable.
It should be something like this:
<ul>
<% cat_opg.roots.each do |cat_opg_root| %>
<li> <%= cat_opg_root.navn %><li>
<% cat_opg_root.children each do |cat_opg_child| %>
<li> <%= cat_opg_child.navn %><li>
</ul>
<% end %>
</ul>
<% end %>
Alex thank you for your answer.
Now it works
Controller: #cat_opg = CatOpg
And the view:
<ul>
<% #cat_opg.roots.each do |cat_opg_root| %>
<li> <%= cat_opg_root.navn %></li>
<% unless cat_opg_root.children.empty? %>
<ul>
<% cat_opg_root.children.each do |cat_opg_child| %>
<li> <%= cat_opg_child.navn %></li>
<% end %>
</ul>
<% end %>
<% end %>
</ul>