Help applying two different styles to Ruby on Rails pluralize method - ruby-on-rails

How do I apply two different styles to the "vote" text and the number output from this RoR code:
<%= pluralize video.vote_sum, 'Vote' %>

Since you want to style the pluralized word, I would just suggest doing the following:
<%= video.vote_sum %>
<div class="style-word">
<% word = "Vote" %>
<% if video.vote_sum > 1 %>
<%= word.pluralize %>
<% else %>
<%= word %>
<% end %>
</div>
(Sorry about all the <% %>, I'm so used to HAML now, I don't do erb anymore).

Try this :
<div class="<%= pluralize video.vote_sum, 'number' %>">
<%= video.vote_sum %>
</div>
<div class="<%= pluralize video.vote_sum, 'vote' %>">
<%= pluralize video.vote_sum, 'Vote' %>
</div>
And in your CSS:
.vote{# some style code here!}
.votes{# some style code here!}
.number{# some style code here!}
.numbers{# some style code here!}

<%= pluralize video.vote_sum, '<span class="some_other_class">Vote</span>', '<span class="some_other_class">Votes</span>' %>

I just ran into this exact situation and chose to extend the pluralize method in application_helper.rb
def pluralize(count, singular, plural = nil)
count, counted = super.split(' ', 2)
[content_tag(:span, count, class: 'count'),content_tag(:span, counted, class: 'counted')].join(' ').html_safe
end
pluralize(2, "Thing") will now output something like <span class="count">1</span> <span class="counted">Things</span>

Related

Rails add text to class in a content_tag_for :li

I have a content tag that is creating jquery sortable output.
Some items I don't want sortable, so I've added the following to the jquery:
cancel: ".ui-state-disabled"
So, now I need to put "ui-state-disabled" into the li class.
Currently the code creating the li is this:
<% wostatus.workorders.each do |workorder| %>
<%= content_tag_for(:li, workorder) do %>
<div class="<%= workorder.type.maximo_no %> <%= workorder.priority %> ">
<a href="<%= workorder_path(workorder) %>">
<strong><%= workorder.wonum %></strong>
<%= workorder.description %>
</a>
</div>
<% end %>
<% end %>
The results in HTML are:
<li class="workorder" id="workorder_36">
<div class=" ">
<a href="/workorders/36">
<strong>13-39870</strong>
Added some text again
</a>
</div>
</li>
In the browser, if I edit the li class to include "ui-state-disabled" it works the way I want.
Now, how can I insert the "ui-state-disabled" into the li status if the workorder.wostatus.id = 232 ??
Thanks for your help!!
Make a helper:
def disabled_workorder_li(workorder)
{:class => "ui-state-disabled"} if workorder.id == 232
end
Then in the view:
<%= content_tag_for(:li, workorder, disabled_workorder_li(workorder) %>
Here's what it would look like if you skipped the helper and tried to do it all in the view:
<%= content_tag_for(:li, workorder,
(workorder.id == 232) ? {:class => "ui_state_disabled"} : nil %>
That looks terrible. Putting it into a helper will also make it easier to test.

Can one use conditions and loops on a single line in Ruby?

How would one go about turning the following code into the latter?
<div id="faqs">
<% if #faqs.length > 0 %>
<% #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end %>
<% else %>
<p>No FAQs to display.</p>
<% end %>
</div>
<div id="faqs">
<% #faqs.empty? ? content_tag(:p, "No FAQs to display.") : #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end %>
</div>
I'm curious as to whether I can get the latter code to work. The only element of it that is failing at the moment is that the content_tag() is not displaying - this is due to the fact that I'm not using printable ruby tags (<%= # %>) but using them will dump out the FAQ objects underneath the content.
I considered the use of puts() to print the content_tag() while inside the ruby tags but that didn't work.
I've tried to search for this issue but haven't yielded anything useful.
Is this achievable and if so, does it have any benefits other than being prettier?
One way to make the later code to work if you can put the body of the loop in a helper function and return the out put of content_tag from that. The line in view file might be somewhat like this.
<%= #faqs.empty? ? content_tag(:p, "No FAQs to display.") : printList(#faqs) %>
and your printList function will return the output of nested content_tags. You can make a generic list printing function which can be used for any list.
Something so obvious but still shared.
This should work (for clarity, I moved FAQ tag generation in separate helper method):
<div id="faqs">
<%= raw (#faqs.empty? ? content_tag(:p, "No FAQs to display.") : #faqs.map { |faq| faq_div(faq) }.join) %>
</div>
or, perhaps more clean:
<div id="faqs">
<%= content_tag(:p, "No FAQs to display.") if #faqs.empty? %>
<%= raw #faqs.map { |faq| faq_div(faq) }.join %>
</div>
meanwhile, in helpers:
def faq_div(faq)
'<div class="faq"><strong>Q:</strong> %s<br /><strong>A:</strong> %s</div>' % [faq.question, faq.answer]
end
This should work:
<% if #faqs.each do |faq| %>
<div class="faq">
<strong>Q:</strong> <%= faq.question %>
<br />
<strong>A:</strong> <%= faq.answer %>
</div>
<% end.empty? %>
<p>No FAQs to display.</p>
<% end %>

Change Text Validation Rails 3

I would like to to do the following, any ideas
I have a Products model with two fields, both strings.
Name
Position
I would like on the index pages for the products to display the Name in Red if the Positions field is blank in the database.
Thanks in advance
Create helper method to check if object is blank?
def set_css_class(object, css_class)
" #{css_class}" if object.blank?
end
Call it in your View:
<div class="name <%= set_css_class(#poroduct.position, 'red') %>">
<%= #product.name %>
</div>
<span class="<%= product.position.nil? ? "red" : "blue" %>">
<%= product.name %>
</span>
Update:
Let's say you have code like you provided in comment, modify it to:
<td>
<%= link_to admin_printer_path(printer), :class => 'ico' do %>
<b<%= ' class="error"' unless printer.position? %>><%= printer.name %></b>
<% end %>
</td>
and in css file/section in header add (or modify according to your structure):
.error { color: red; }

Style tag_list class from Acts as Taggable On

I'm undoubtedly missing something obvious, but how should I style the output of individual tags produced by <%= user.tag_list %>?
<%= user.tag_list, :class => 'tags' %> doesn't work...
Note, I want to style the individual results, not the whole block.
Thanks!
This should do the trick:
<% user.tag_list.each do |tag| %>
<span class="tags"><%= tag %></span>
<% end %>

How do I wrap link_to around some html ruby code?

How do I wrap a link around view code? I can't figure out how to pass multiple lines with ruby code to a single link_to method. The result I am looking for is that you click the column and get the show page:
<div class="subcolumns">
<div class="c25l">
<div class="subcl">
<%= image_tag album.photo.media.url(:thumb), :class => "image" rescue nil %>
</div>
</div>
<div class="c75r">
<div class="subcr">
<p><%= album.created_at %></p>
<%= link_to h(album.title), album %>
<p><%= album.created_at %></p>
<p><%= album.photo_count %></p>
</div>
</div>
</div>
link_to takes a block of code ( >= Rails 2.2) which it will use as the body of the tag.
So, you do
<%= link_to(#album) do %>
html-code-here
<% end %>
But I'm quite sure that to nest a div inside a a tag is not valid HTML.
EDIT: Added = character per Amin Ariana's comment below.
Also, this may be an issue for some:
Make sure to write <%= if you are doing a simple link with code in it instead of <%.
e.g.
<%= link_to 'some_controller_name/some_get_request' do %>
Hello World
<% end %>
For older Rails versions, you can use
<% content_tag(:a, :href => foo_path) do %>
<span>Foo</span>
<% end %>
You can use link_to with a block:
<% link_to(#album) do %>
<!-- insert html etc here -->
<% end %>
A bit of a lag on this reply I know -- but I was directed here today, and didn't find a good answer. The following should work:
<% link_to raw(html here), #album %>

Resources