I'm trying to print out a string with embedded ruby to a webpage, if I use the following it ends up going off of the page.
<%= comment.body %>
I want to do something like this so that it automatically prints everything with new lines
<%= comment.body[0..136] %>
<%= "\n" %>
<%= comment.body[137..250] %>
I've done the following however it prints the characters with spaces between them and ignores spaces in the string.
<% for i in 0..comment.body.length do %>
<%= i %>
<% end %>
Thanks
i'm not sure but it can help you some how
<%=simple_format(comment.body, html_options={ class: 'your_class'}, options={})%>
for more details and modification follow this
Why not use HTML?
<% for i in 0..comment.body.length do %>
<%= i %><br>
<% end %>
Solved it by using CSS
in HTML:
<p id="commentBody">
...
</p>
in CSS:
#commentBody {
word-wrap: break-word;
}
Related
I am trying to underline words that are dynamically generated by the debug(params) method provided by rails. I have something below, but it obviously does not work, plus what I have below is attempt to try and change the words using methods that I already know about (like the .upcase method). I was hoping to underline the word controller if it appears in the text using only Ruby. Can anyone help me out here?
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<%= 'controller'.upcase %>
<% end %>
thanks
edit:
I should add that debug(params) is a method defined by RAILS, I was able to do the following which seems even more off, so far the answers have not been correct to what I want to do.
<% if Rails.env.development? %>
<% debug_method = debug(params).split.each do |word| %>
<% if word == 'controller:' %>
<ul><% word.upcase %></ul>
<% end %>
<% end %>
<%= debug_method.join %>
<% end %>
which returns the following text: https://ibb.co/cvnEpw , keep the answers coming in though. I want to get the words in the original box (that's generated by the method to underline the controller word https://ibb.co/jmSm2G).
use <u></u> tag
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<u><%= 'controller'.upcase %></u>
<% end %>
example here
Provide the css to generate html element:
p { text-decoration: underline; }
Add html elemnt to wrap your words:
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<p> <%= 'controller'.upcase %> </p>
<% end %>
The answer to the question is below. I had to use the .gsub and .html_safe methods.
<%= debug(params).gsub("controller:", "<u>controller:</u>").html_safe %>
This code keeps the existing html & css generated by rails intact
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 %>
OK, stupid newbie question: how do you make a line break only when it's needed?
I'm creating a basic address listing and only want to include a line of an address if it isn't blank. How do I keep the blank line from printing? I've tried including the break and new line tags, and tried using puts and quotation marks of both varieties and escaping the slashes but can't seem to display the address correctly.
Is there a way to have each line of the address to print on its own line or simply omit the line if there is no info to put on it?
Here's the current version of the code:
<p><strong>Main Address</strong></p>
<p><%= if #vendor.address1 || null
#vendor.address1 #need a break here
end %>
<%= if #vendor.address2 || null
#vendor.address2 #need a break here
end %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %></p>
This is how I would do it:
<p>
<strong>Main Address</strong>
</p>
<p>
<% unless #vendor.address1.blank? %>
<%= #vendor.address1 %><br>
<% end %>
<% unless #vendor.address2.blank? %>
<%= #vendor.address2 %><br>
<% end %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %>
</p>
By the way: the || null in your code is not valid Ruby. null does not exist, it should be nil. But even if you had used nil, your code does not do what you expect it to do. For these kind of things, you'd better use blank?.
<p><strong>Main Address</strong></p>
<p><%= #vendor.address1%><%= <br/> if #vendor.address1.blank? %>
<%= #vendor.address2%><%= <br/> if #vendor.address2.blank? %>
<%= #vendor.city %>, <%= #vendor.state %> <%= #vendor.zip %></p>
I have a repeated line which outputs something like:
Call John Jones in -3 days (status)
I have a helper called show_status(contact,email) which will output whether that particular email had been sent to that particular contact.
If it is "sent," then that entire line should show up as "strike out."
Similarly, if the number of days is -3 (<0), the line should be formatted in red.
Here's my hack, but there must be a cleaner way to put the logic into the controller?
I hard-code a value that wraps around the lines I want formatted, and assign the value based on a separate call to the same helper:
<% for call in #campaign.calls %>
<% if !show_call_status(#contact,call).blank? %>
<%= strike_start = '<s>'%>
<%= strike_end = '</s>' %>
<% end %>
<p>
<%= strike_start %>
<%= link_to call.title, call_path(call) %> on
<%= (#contact.date_entered + call.days).to_s(:long) %> in <%= interval_email(#contact,call) %>
days
<%= make_call(#contact,call) %>
<span class='status'><%= show_call_status(#contact,call) %></span>
<%= strike_end %>
</p>
<% end %>
I guess what I'd like to do is not have the if statement in the View. Not sure how to do this.
Basically, I would put a class on the p tag based on the status and have the CSS decide what needed to be done.
So the view:
<% for call in #campaign.calls %>
<p class="<%= call_status_class(#contact, call) %>">
<%= link_to call.title, call_path(call) %> on
<%= (#contact.date_entered + call.days).to_s(:long) %> in <%= interval_email(#contact,call) %>
days
<%= make_call(#contact,call) %>
<span class='status'><%= show_call_status(#contact,call) %></span>
</p>
<% end %>
And another helper:
def call_status_class(contact, call)
# do what you have to do to figure out status
if overdue
return 'overdue'
elsif called
return 'called'
else
return 'standard'
end
end
Then in CSS:
.standard {
...
}
.overdue {
color: red;
}
.called {
text-decoration: line-through;
}
Pick and choose. I can't really give you full fledged solution without seeing all the helper functions. Hope this helps.
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 %>