I'm trying to change the colour of a bootstrap list item based on whether the correct answer to a question is selected...but using link_to is making it complicated. Advice appreciated. Thx.
<ul class="list-group" id="answered">
<% #question.answers.each do |answer| %>
<%= link_to(answer.text, question_path(#question, attempted_answer: answer.id), :class => "list-group-item") %>
<% if #attempted_answer && #attempted_answer == answer %>
<% if #attempted_answer.correct %>
correct
<% else %>
incorrect
<% end %>
<% end %>
Create a method in helper file as below
def correct_answer(attempted_answer, answer)
return false unless attempted_answer
attempted_answer == answer && attempted_answer.correct
end
Then call helper method in your view
<%= link_to(answer.text, question_path(#question, attempted_answer: answer.id), :class => "list-group-item #{correct_answer?(#attempted_answer, answer) ? 'list-group-item-success' : 'list-group-item-danger'}") %>
<% correct_answer?(#attempted_answer, answer) ? 'correct' : incorrect %>
<% end %>
Related
I'm trying to find a way to display in a View the badge & the "granted_at" record. It would render (in the view) :
<% #profil.badges.each do |badge| %>
<%= image_tag (badge.custom_fields[:image]), badge.granted_at %>
<% end %>
Should I enable the MeritObserver to get this on the view ? Or there is a simpler solution?
(I'm on Rails 5)
EDIT
Thanks to TuteC, we have an answer :
<% #profil.sash.badges_sashes.each do |badge_sash| %>
<%= image_tag (badge_sash.badge.custom_fields[:image]) %><%= badge_sash.created_at %>
<% end %>
You've got to go through sash and badges_sashes relations:
<% #profil.sash.badges_sashes.each do |badge_sash| %>
<%= image_tag(badge_sash.badge.custom_fields[:image]), badge_sash.created_at %>
<% end %>
You can read about merit internals in https://github.com/merit-gem/merit/wiki/General-merit-workflow.
undefined method `each' for nil:NilClass
<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
<nav id="taxonomies" class="sidebar-item" data-hook>
<% #taxonomies.each do |taxonomy| %>
<% unless taxonomy.name == 'Tags' %>
<h6 class="taxonomy-root"><%= Spree.t(:shop_by_taxonomy, :taxonomy => taxonomy.name) %></h6>
<%= taxons_tree(taxonomy.root, #taxon, Spree::Config[:max_level_in_taxons_menu] || 1) %>
This is the error i am getting and this is the code i updated to taxonomies.each.to_a not sure if that is right and how do i update the code so the server will run the new code instead of the older code?
Here is the updated code
<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
<nav id="taxonomies" class="sidebar-item" data-hook>
<% #taxonomies.each.to_a do |taxonomy| %>
<% cache [I18n.locale, taxonomy, max_level] do %>
<h6 class='taxonomy-root'><%= Spree.t(:shop_by_taxonomy, :taxonomy => taxonomy.name) %></h6>
<%= taxons_tree(taxonomy.root, #taxon, max_level) %>
<% end %>
<% end %>
</nav>
Looks like what you're doing is forcing a nil object to an array, which will work, yt isn't an ideal solution. You can wrap the whole thing in this:
<% if #taxonomies && #taxonomies.any? %>
#your code goes here
<% end %>
...which will check for the presence of a taxonomies variable and make sure it isn't an empty array before running your code. As far as getting the code on the server goes, you'll have to post more info about your deployment setup and version control to answer that question.
I have the following logic in my view to choose which avatar picture to show depending on whether a persons profile is present
<% if #profile %>
<%= image_tag(#profile.avatar_url(:thumb)) %>
<% else %>
<%= image_tag(default_image_url) %>
<% end %>
Helper method
def default_image_url
hash = Digest::MD5.hexdigest(current_user.email)
"https://secure.gravatar.com/avatar/#{hash}?s=100&d=mm"
end
This works fine when someone has not created a profile, but when they do and still want to use their Gravatar this logic fails as my if condition then needs to be if
<% if #profile.avatar? %>
<%= image_tag(#profile.avatar_url(:thumb)) %>
<% else %>
<%= image_tag(default_image_url) %>
<% end %>
At the moment when a profile is created with no image uploaded by the user, there is no image displayed at all.
How can I cover all scenarios?
Edit
I'm in the process of trying
<% unless #profile || #profile.avatar %>
A bit of refactoring starting from #ArieShaw's answer:
Helper
def profile_image_url
#profile.try(:avatar?) ? #profile.avatar_url(:thumb) : default_image_url
end
View
<%= image_tag profile_image_url %>
You may use Object#try:
<% if #profile.try(:avatar?) %>
<%= image_tag(#profile.avatar_url(:thumb)) %>
<% else %>
<%= image_tag(default_image_url) %>
<% end %>
When you go to blog page, you will see the archive list on the menu.
In most cases, it shows something like this
'Archive'
2012(78)
-December(1)
-November(5)
-October(10)
...
2011(215)
2010(365)
I'm confident to make blog posting system by using scaffold.
But I have no idea how to make this Archive:(
Anyone come up with good idea to implement this to app easily???
Need your help!!
<h3>Archives </h3>
<% if #posts.to_a.empty? %>
<div class="post">
<p>No articles found...</p>
</div>
<% else %>
<% current_month = 0 %>
<% current_year = 0 %>
<% for article in #posts %>
<% if (article.created_at.year != current_year)
current_year = article.created_at.year
%>
<h3 class="archiveyear"><%= article.created_at.year%></h3>
<% end %>
<% if (article.created_at.month != current_month || article.created_at.year != current_year)
current_month = article.created_at.month
current_year = article.created_at.year
%>
<h4 class="archivemonth"><%= (Date::MONTHNAMES[article.created_at.month]) %></h4>
<% end %>
<div class="archivepost">
<%= link_to article.title, article_path(article), :remote => true %> on <%= article.created_at.strftime('%A')%> - <%= article.created_at.strftime('%d') + "th"%>
</div>
<% end -%>
<%end %>
This may help you. I ve not included the number of counts in this code. Actually m figuring how to do it. If u can let me know.
Also in the controller ive done this.
#posts = Article.order("created_at DESC")
The #posts is an array so the items inside it ll be ordered and then i can fetch the records according to it ordering.
Thanks.
I imagine this has a rather simple answer
<% for user in #users %>
<li>
<%= link_to user.username, user %>
</li>
<% end %>
Taking my simple example above, how would I apply a class to my <li> to the first three instances returned?
Secondly, how could I just have the the second two items have a different class from the first one? as in 1..2
Either you could count manually (which is kinda ugly):
<% i = 0 %>
<% for user in #users %>
<li class=<%= (i < 3 ? "foo" : "bar") %>>
<%= link_to user.username, user %>
</li>
<% i = i.next %>
<% end %>
or use each_with_index
<% #users.each_with_index do |user, i| %>
<li class=<%= (i < 3 ? "foo" : "bar") %>>
<%= link_to user.username, user %>
</li>
<% end %>
Once you get to more complex things than i < 3 (like your 1..2 issue) you should think about a helper_method (in helpers) like class_by_position(pos) so that you can write
<li class=<%= class_by_position(i) %>>
The :first-child pseudoselector might be a better way to go, but you'll need to have a counter variable that keeps track of the iterations to do it your way.
Your question is a little vague. I can't tell if you want to stop processing the array after the first x, or not.
If you're just looking to stop after the first x items, or just looking for the 2nd and 3rd items the solution is to use a slice.
Example: just the first 3:
#user[0,3].each do |user|
... # only executed for user = #user[0],user = #user[1] and user = #user[3]
end
Example: just the second and third:
#user[1,2].each do |user|
... #only only executed for user = #user[1] and user = #user[3]
end
And here's a more specific answer to your question using these new concepts and the content_tag to programatically decide the class of the list item. If you're going to be doing this often, makes a great candidate for a function.
<% first = true %>
<% #user[0,2].each do |user| %>
<% content_tag :li,
:class => first ? "class-for-first-item" : "class-for-2nd-&-3rd" do %>
<%= link_to user.username, user %>
<% end %>
<% first = false %>
<% end %>
<!-- Now to do the rest of them:-->
<% #user[3,-1].each do |user| %>
<% content_tag :li, :class => "class-for-rest" do %>
<%= link_to user.username, user %>
<% end %>
<% end %>