Nested <span> tag inside link_to in Rails 7 - nested

I need the following HTML output exactly as shown below:
<section id='option1'>
<a href='#option1'><h6>Option1</h6><span>></span></a>
<div class='content'>Option1 content...</div>
</section>
I am using a Ruby block to create multiple sections like the one shown above in order to build an accordion UI. However, the problem is that the <span> tag won't nest inside the <a> tag properly. I have checked out other SO queries on the subject, but can't seem to find a solution.
What I hope to end up with is a Ruby block that looks something like this:
<% %w[option1 option2].each do |act| %>
<%= tag.section do %>
<%= link_to("#{act}".capitalize, "##{act}", {class: 'centered', data: { turbo_frame:"content"}}) do %>
<%= tag.h6 act do %>
<%= tag.span raw ">" %>
<% end %>
<% end %>
<% end %>
<% end %>
But I get a undefined method 'stringify_keys' for "option1":String
Can someone help me construct a nested content_tag that puts all the required elements in the right place?

Ok, so with some fiddling with the HTML structure, I came up with the following that works:
<div id='accordion'>
<% %w[option1 option2 ... optionN].each do |act| %>
<%= tag.h2(id:"accordion_header", class: "ui-accordion-header") do %>
<%= link_to("#{act}".capitalize, "#{act}") %>
<% end %>
<%= tag.div(id: "#{act}", class: "ui-accordion-content") do %>
<%= render "/sidebars/content/#{controller_name}/#{act}", formats: :html, handlers: :erb %>
<% end %>
<% end %>
</div>
<script>
$("#accordion").accordion();
</script>
I still haven't figured out how to get the anchor tags to respond to the click event and render the appropriate content via turbo-frames yet, so feel free to comment on how to do that. The documentation for Turbo is (at the present) woefully inadequate.

Related

Does 'substring' work directly in an EJS file

<%- include("partials/header"); -%>
<h1>Home</h1>
<p><%= startingContent %></p>
<% posts.forEach(function(post){ %>
<h1><%=post.title%></h1>
<p>
<%=post.content.substring(0, 100) + " ..."%>
<!-- This line's giving me an error, I dont know why -->
Read More
</p>
<% }) %> <%- include("partials/footer"); -%>
Normally, it's supposed to reduce the number of characters to be displayed to a specific number(in this case 100).

I am using Node.JS and there is a mistake in expecting ')'. Please how can i solve it

`<h2>Hobbies</h2>
<ul>
<%= data.hobby.forEach(function(item)
{ %>
<li>
<%= item %>
</li>
<%
}); %>
`
here is the mistake : SyntaxError: Unexpected token ')' in C:\src\express\views\profile.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
why you have added = before foreach.that's the issue. remove that = sign. & you code will work.
`<h2>Hobbies</h2>
<ul>
<% data.hobby.forEach(function(item)
{ %>
<li>
<%= item %>
</li>
<%
}); %>
Reference:
https://ejs.co/#docs
Instead of using "%>" and "<%" inside the function I would make it return a string with the desired content.

How to enumerate through an object's properties in EJS

I am trying to get for Each to enumerate through an object's properties and show up unique results. For example I'm trying to iterate through all the messages in a server and only display those where the current user is a sender
. And while doing that I want, to pull up all the receivers, create a group for each and display every message that interacts with that receiver. However I can only get to the point where I am displaying all messages but if a receiver comes twice it will create two separate groups with the same receiver instead of one.
I've tried to enumerate through the properties, but I get an error saying that you cannot apply a for Each on an object's properties.
<% message.forEach(function(message){%>
<% message.sender.id.forEach(function(message){ %>
<% if(currentUser._id.equals(message.sender.id)) {%>
<div>
<p>Message sent to <%= message.receiver.username %></p>
<ul>
<li><%= message.text %></li>
</ul>
</div>
<% }); %>
<%}%>
<%});%>
I'm expecting a div with each receiver and its correspondence instead I get multiple groups(one group per message found with the same receiver
I think you misplaced a bracket. Please try code below.
suggested code
<% message.forEach(function(message){%>
<% message.sender.id.forEach(function(message){ %>
<% if(currentUser._id.equals(message.sender.id)) {%>
<div>
<p>Message sent to <%= message.receiver.username %></p>
<ul>
<li><%= message.text %></li>
</ul>
</div>
<%};%> //I moved a bracket from this line to the next line
<%})%>
<%});%>

My EJS include causes error in my express app.

I am trying to make my site more component based using includes in my application but it just throws an error to the page when I reload it. I have tried:
<%=include _partials/site-head/site-head %>
And I have tried:
<%=include virtual="_partials/site-head/site-head" %>
Here is the code.
<%=include virtual="_partials/site-head/site-head" %>
<h1><%= title %></h1>
<% for(var i=0; i<userlist.length; i++) {%>
<p><%= userlist[i].name %></p>
<% } %>
<%=include virtual="_partials/site-foot/site-foot" %>
Tag <%= is used to output variables, for code need to use <%- %>. So to include views you need to write like this:
<%- include _partials/site-head/site-head.ejs %>
<h1><%= title %></h1>
<% for(var i=0; i<userlist.length; i++) {%>
<p><%= userlist[i].name %></p>
<% } %>
<%- include _partials/site-head/site-foot.ejs %>
Also read this answer.
My Answer that solved this was to check what version of express your using, I was using an old version which didnt like include.

Search result displays on wrong page

I am having a bit off difficulty implementing the ransack gem I have a pages controller with an index action and post controller also with an index action. However when I perform a search in the index action of the pages controller (pages#index) the results are rendered in the index action of the post controller(posts#index). Does this mean i can only search from within the views of a particular resource or am I making a mistake ?
pages#index
def index
#q = Post.search(params[:q])
#posts = #q.result(distinct: true)
end
pages#index
<%= search_form_for #q do |f| %>
<div class="field">
<%= f.label :title_cont %><br>
<%= f.text_field :title_cont, :class => "input text" %>
</div>
<div class="actions">
<%= f.submit "Search"%>
</div>
<% end %>
<%= search_form_for #q,:url=>pages_path do |f| %>
<div class="field">
<%= f.label :title_cont %><br>
<%= f.text_field :title_cont, :class => "input text" %>
</div>
<div class="actions">
<%= f.submit "Search"%>
</div>
<% end %>
Modify the code in the search form like this. It will work now.

Resources