Whats wrong with this else if statement? - ruby-on-rails

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.

Related

How to show two images in one slide

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>

How to improve code in Views on Ruby on Rails

I have code in file views name: _result.html.erb, this file is rendered from file show.html.erb, both file in one folder
File _result.html.erb
<% if #lesson.answers.at(f.index).is_correct %>
<% if #lesson.answers.at(f.index).id == answer.id %>
<li class="text-success">
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
<% else %>
<li>
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
<% end %>
<% else %>
<% if #lesson.answers.at(f.index).id == answer.id %>
<li class="text-danger">
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
<% else %>
<li>
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
<% end %>
<% end %>
And I want to improve this code in file _result.html.erb for shorter, help me please!!!
File show.html
<% provide :title, t("start_lesson") %>
<h1><%= #course.name %></h1>
<h2><%= #course.description %></h2>
<h3><%= t "title_question" %></h3>
<% if #lesson.finished.present? %>
<h4>
<%= t "score" %>:
<%= #lesson.results.is_correct_answers.count %> /
<%= #lesson.words.count %>
</h4>
<% end %>
<%= form_for [#course, #lesson] do |f| %>
<%= f.fields_for :results do |builder| %>
<ul class="list-unstyled">
<li>
<%= "#{builder.index + 1}." %>
<%= #words.at(builder.index).content %>
</li>
<ul class="list-unstyled">
<% #words.at(builder.index).answers.each do |answer| %>
<% if #lesson.finished.nil? %>
<li>
<%= answer.content %>
<%= builder.radio_button :answer_id, answer.id %>
<%= builder.hidden_field :word_id, value: answer.word.id %>
</li>
<% else %>
<%= render "result", f: builder, answer: answer %>
<% end %>
<% end %>
</ul>
</ul>
<% end %>
<% if #lesson.finished.nil? %>
<%= f.submit t("submit"), class: "btn btn-primary" %>
<% end %>
<% end %>
This is just a code-rewrite exercise, so here goes:
result.html.erb
<% if #lesson.answers.at(f.index).id == answer.id %>
<% if #lesson.answers.at(f.index).is_correct %>
<li class="text-success">
<% else %>
<li class="text-danger">
<% end %>
<% else %>
<li>
<% end %>
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
or the shorter (but much harder to read) version:
<li<% if #lesson.answers.at(f.index).id == answer.id %>class="<%= #lesson.answers.at(f.index).is_correct ? "text-success" : "text-danger" %>" <% end%>>
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
In the first example, the condition was inverted to take advantage of the fact that the else condition in both interior conditions was identical, and then using the conditions only to style the <li> node, since that was the only difference between the 4 blocks.
The second example goes further and does the conditional checks inline with the element, building the class attribute when necessary. This is much harder to read at a glance, but is far more compact.
show.html
<% provide :title, t("start_lesson") %>
<h1><%= #course.name %></h1>
<h2><%= #course.description %></h2>
<h3><%= t "title_question" %></h3>
<% if #lesson.finished.present? %>
<h4><%= "#{t 'score'}: #{#lesson.results.is_correct_answers.count} / #{#lesson.words.count} %></h4>
<% end %>
<%= form_for [#course, #lesson] do |f| %>
<%= f.fields_for :results do |builder| %>
<ul class="list-unstyled">
<li><%= "#{builder.index + 1}.#{#words.at(builder.index).content}" %></li>
<ul class="list-unstyled">
<% #words.at(builder.index).answers.each do |answer| %>
<% if #lesson.finished.nil? %>
<li>
<%= answer.content %>
<%= builder.radio_button :answer_id, answer.id %>
<%= builder.hidden_field :word_id, value: answer.word.id %>
</li>
<% else %>
<%= render "result", f: builder, answer: answer %>
<% end %>
<% end %>
</ul>
</ul>
<% end %>
<% if #lesson.finished.nil? %>
<%= f.submit t("submit"), class: "btn btn-primary" %>
<% end %>
<% end %>
Most of the changes here are just using string interpolation to combine string elements that are otherwise multiple <%= %> blocks.
The larger loop portion that builds the list of answers has some possible issues. For instance, the rendered result (when the lesson is not finished) is missing the <li> and </li> elements, so the answers are simply sitting as a blob of text within the enclosing <ul>. In order to preserve that, in case it was intentional, most of the middle block remained.
However, this is probably not what you actually wanted, so this version fixes that and does a little more reorganization:
<% provide :title, t("start_lesson") %>
<h1><%= #course.name %></h1>
<h2><%= #course.description %></h2>
<h3><%= t "title_question" %></h3>
<% if #lesson.finished.present? %>
<h4><%= "#{t 'score'}: #{#lesson.results.is_correct_answers.count} / #{#lesson.words.count} %></h4>
<% end %>
<%= form_for [#course, #lesson] do |f| %>
<%= f.fields_for :results do |builder| %>
<ul class="list-unstyled">
<li><%= "#{builder.index + 1}.#{#words.at(builder.index).content}" %></li>
<ul class="list-unstyled">
<% #words.at(builder.index).answers.each do |answer| %>
<li>
<% if #lesson.finished.nil? %>
<%= answer.content %>
<%= builder.radio_button :answer_id, answer.id %>
<%= builder.hidden_field :word_id, value: answer.word.id %>
<% else %>
<%= render "result", f: builder, answer: answer %>
<% end %>
</li>
<% end %>
</ul>
</ul>
<% end %>
<% if #lesson.finished.nil? %>
<%= f.submit t("submit"), class: "btn btn-primary" %>
<% end %>
<% end %>
That's about the shortest this can be without collapsing the structure or just removing whitespace. Each remaining element seems to have a purpose and is placed where it should be, without redundancy or over-complication.

Rails view returning database field after loop

In my view I do this:
<ul class="list-group">
<%= JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
It lists everything out like it should, but then at the end it returns the value of current_printer.stripe_managed_account.fields_needed. I'm not sure why it does this, or how to prevent it from doing this.
This is a screenshot:
Replace <%= %>, on your each line by <% %>.
Because <%= #your each %> is same that : <% puts #your each %>.
Try this:
<ul class="list-group">
<% JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
Thanks #vee

Rails: Cloudinary gem - default image not loading

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 %>

link_to in ruby on rails with additional html

I know there are more of these, but I couldn't find my answer as I'm still fairly new to RoR.
I need to take this:
<% if params[:forum_id] %>
<%= link_to "#{category.name}", category_path(category.id,:forum_id => params[:forum_id]) %>
<% else %>
<%= link_to "#{category.name}", category_path(category.id) %>
<% end %>
which prints out:
name
and I need:
<a href="mylink....">
<figure></figure>
<span>name</span>
</a>
Thanks!
You can use link_to as a block:
<%= link_to category_path(category_id) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
EDIT
The full solution:
<% if params[:forum_id] %>
<%= link_to category_path(category.id,:forum_id => params[:forum_id]) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
<% else %>
<%= link_to category_path(category.id) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
<% end %>

Resources