Object rendering as text in Rails 7 web app - ruby-on-rails

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

Related

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

Ruby on Rails - Underline words if they appear in dynamically generated text using Ruby

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

Rails how to render collection inside form_for?

I want to render a collection of characters inside a form_for (so I can message multiple characters at once using check_box_tag inside each character_item). I'm using the following code but it is faulty. Only the first character is rendered inside the form, the rest appear outside the form. What am I doing wrong?
<%= form_for(:message, url: :messages, method: :post, html: { id: "message_form" }
) do |f| %>
<% if characters.any? %>
<% characters.each do |character| %>
<%= render "characters/character", character: character, f: f %>
<% end %>
<% end %>
<% end %>
This produces the following html:
<form id="message_form" enctype="multipart/form-data" action="/messages" accept-charset="UTF-8" method="post">
<div class="character_item" id="character_alfred">
</form>
<div class="character_item" id="character_barney">
<div class="character_item" id="character_chas">
<div class="character_item" id="character_dean">
EDIT
I've narrowed the problem down to this block of code. The layout is fine when I remove this:
<%= button_to destroy_relationship_path(
params: {
following_callsign: #character.callsign,
followed_callsign: followedcallsign,
viewed_callsign: viewedcallsign
}
),
class: 'btn btn-default btn-xs post_button',
id: 'unfollow-eye',
remote: true,
method: :delete do %>
<span class="glyphicon glyphicon-sunglasses" aria-hidden="true"></span>
<% end %>
Can anyone see what's wrong with it? I suspect there isn't anything wrong with the button code, I think it's something to do with having a button inside a form. I don't know. I am, as usual, baffled.
Turns out you can't put a button inside a form, because it closes the form. See this answer:
https://stackoverflow.com/a/34644553/346613
The solution is to convert all the button_to into link_to.

How can I change the tags/css class of some dynamic text based on output from a helper?

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