how to truncate and add a read more in rails - ruby-on-rails

hey guys i have this piece of code
<%#=link_to raw page.body%></a></div>
<div class='col-md-8'>
<div class="container">
<h4><b> <%= link_to page.title, page_path(page.id)%> </b></h4>
<p><%=link_to raw page.body%></p>
</div>
</div>
<% end %>
i want to truncate the page.body output so after 200 text, it'll truncate it and add a read more button to view the full page.
How do i do it please

The ActionView::Helper::TextHelper#truncate might work for that:
<%= link_to truncate(page.body, length: 200) { link_to 'Read More', '#' } %>
The block passed permits you pass an additional link_to helper which you can make work with JS or any other as you need.

Related

Object rendering as text in Rails 7 web app

Would anyone be able to tell me why this text is showing up out of the blue on my Rails 7 app. I have poured over the scss, js, and html unable to find what is rendering it. Additionally the app contains another object (called employee and not template) and used exactly the same in the source code but does have this plain text added to it.
The only difference between them is that I am using this object in a modal vs no modal but both are using turbo frames. I would be happy to post some code but I thought someone might easily recognize it first.
Here is the partial.
<%= #templates.each do |temp| %>
<div class="template__actions">
<div class="line-item__name">
<%= temp.name %>
</div>
<%= link_to "<i class='bi bi-trash'
style='font-size: 1.5rem; color:
red;'></i>".html_safe,
[temp],
data: {"turbo-method": :delete},
form: { data: {turbo_frame: "_top" } },
class:"btn btn--light staff_icon" %>
<%= link_to "<i class='bi bi-pencil-square'
style='font-size: 1.5rem; color:
black;'></i>".html_safe,
[:edit, temp],
class:"btn btn--light staff_icon" %>
</div>
<% end %>
This is your problem:
<%= #templates.each do |temp| %>
The equal sign makes it print the template array.
Change it to:
<% #templates.each do |temp| %>`
You are printing the loop in your erb syntax.
<%= is to print the return and you are implicitly returning the collection at the end of the loop.
Instead just use <% to declare the erb and not print the return.
change line 1
<%= #templates.each do |temp| %>
to
<% #templates.each do |temp| %>

Strange output from rails each do

Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>

html_safe doesn't work when I call truncate() method

I have a simple RSS feed for a website I'm trying to build. I want to do a read more link for posts and limit the output to 300 characters. My code looks like this:
<p id="notice"><%= notice %></p>
<h1 class="reviews">What They're Saying About Us</h1>
<br />
<h1 class="post-title"><%= image_tag("icons8-rss-40.png") %>RSS Feed</h1>
<% #home_blogs.each do |p| %>
<div class="blog-posts">
<h3><%= p.name %></h3> | <%= p.created_at.to_date %>
<br />
<p id="blog_post_content">
<% if p.entry.length > 200 %>
<%= truncate(p.entry, length: 300).html_safe %>
<%= link_to "...read more", p %>
<% else %>
<%= p.entry.html_safe %>
</p>
<% end %>
<br />
<%= image_tag("if__hashtag_2559811.png")%>Tag Cloud
<div class="tag-cloud">
</div>
</div>
<% end %>
<span class="pagination"><%= will_paginate #home_blogs %></span>
<br>
For whatever reason the html tags are included when I create a post even though I'm doing html_safe. Any ideas? Thanks
truncate automatically marks the output as html_safe and also escapes the content by default. This means your link is probably being escaped before you even get a chance to mark it as html_safe yourself.
You can pass an escape: false option to truncate in order to get it to skip the escaping that it does by default.
E.g.
<%= truncate(p.entry, length: 300, escape: false) %>
From the docs for truncate
The result is marked as HTML-safe, but it is escaped by default, unless :escape is false. Care should be taken if text contains HTML tags or entities, because truncation may produce invalid HTML (such as unbalanced or incomplete tags).

How should we combine the div with link_to in Rails?

My original intention was to display some text on the image. At the same time, when we click the images, the webpage will be redirected.
And I use link_to function with div containing background image.
The code is like this:
<%= link_to raw('<div style="background-image:url(<%= image_url '1.jpg'%>);width:340px;"> This is a test</div>'),index_by_dvp_domain_path %>
But the system tells me there is SyntaxError.
You can pass link_to a block that contains the content you want to display. So instead of going with the link_to(display, url, options={}) you get link_to(url, option={}, &block) where you can do.
<%= link_to index_by_dvp_domain_path do %>
<div style="background-image: url(<%= image_url '1.jpg'%>);width:340px;">
This is a test
</div>
<% end %>
After you do this you can treat it like normal html.
As always, I'd recommend trying to move any style out into it's own separate stylesheet.
Best way to do it this is used following
<%= link_to index_by_dvp_domain_path do
content_tag(:div, 'This is a test',:style=>"background-image:url(#{image_url} '1.jpg');width:340px;" )
end
%>
OR
<%= link_to content_tag(:div, 'This is a test',:style=>"background-image:url(#{image_url} '1.jpg');width:340px;" ), index_by_dvp_domain_path %>
Please have a try with
<%= link_to raw('<div style="background-image:url(#{image_url '1.jpg'}%>);width:340px;"> This is a test</div>'),index_by_dvp_domain_path %>
I think using Link_to as below would be much more simpler even when you have a big block including multiple tags:
<%= link_to desired_path do %>
<div class="linkable">
<another div>
... some other tags
</another div>
</div>
<% end %>
and I recommend you to use a different background color for mouse over events because it shows the viewer that it's a link!
In you .css file:
.linkable:hover{
background-color: red;
}
Im so surprised to see that no one came up with the regular way of doing it in Rails.
<%= link_to image_tag("/images/1.jpg",:size => "340X340"),index_by_dvp_domain_path %>

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