Rails encoding special characters within double quotes

I'm facing an issue with setting my own percentage in a Bootstrap progress bar. The float value is being passed to the view correctly, but for some reason, the Bootstrap progress bar is not reading it. I suspect that there might be an error in how I am handling the style tag.

This is what the view looks like:

<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="#{ <% @app.completion_status %> }" aria-valuemin="0" aria-valuemax="100" style="width: '#{raw(<%= number_to_percentage(@app.completion_status) %>)}';">
    <%= @app.completion_status %>%
  </div>
</div>

Upon closer inspection:

style="width: '#{raw(<%= number_to_percentage(@app.completion_status) %>)}';"

The value that needs to be converted to a percentage is: `33.3333333333333` (one third)

This is how the controller is set up:

def set_completion
  @app = App.find(params[:id])

  @app.update_attribute(:completion_status,

  (((@app.elements.where(completion: true).count) / (@app.elements.count).to_f) * 100 )

  )
end

Answer №1

Upon initial inspection, it appears that you are utilizing ruby interpolation outside of the erb tags, which may be causing the issue. Edit: Please try the code below

style="width: <%= number_to_percentage(@app.completion_status) %>"

Here is an example I just created for you to reference. Remember to keep your interpolation within the ERB tags!

<div class="progress-bar progress-bar-info progress-bar-striped"
 role="progressbar" aria-valuenow="<%=@email_usage%>" aria-valuemin="0"
 aria-valuemax="100" style="width: <%=@email_usage%>%">

Answer №2

Have you attempted to switch the positions of ' and "?

<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="#{ <% @app.completion_status %> }" aria-valuemin="0" aria-valuemax="100" style='width: "#{raw(<%= number_to_percentage(@app.completion_status) %>)}";'>
    <%= @app.completion_status %>%
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to prevent flex from expanding all the way to 100% width?

My section has two columns centered in flexbox, but the container doesn't wrap around them as expected and instead fills the whole page. How can I make the section wrap around the text with a padding of 1rem? body { background-color: #444; col ...

What is the best way to smoothly transition between three images, each appearing in a randomized position and size?

Looking to add a dynamic div that fades in and out randomly with varying sizes and positions? You may have already tried displaying three images with fade-in and fade-out effects, but struggled with randomizing their placement and size. Check out this ex ...

"Modify the CSS color of h1 based on the presence of a specific class containing spaces in a div

I am currently attempting to modify the color of a heading by referencing a specific div's class. My attempts so far include: .pagetitle-title.heading { color: purple; } <div class="container"> <h1 class="pagetitle-title heading">I ...

What causes certain properties of html and body elements to behave in this way?

I'm on a quest for understanding some mysteries. First riddle: Why does the body have a mysterious margin-top? html { height: 100%; background-color: red; } body { height: 100%; margin: 0; background-color: yellow; } <h1>Hello P ...

CSS: Implementing opacity on background images within an unordered list menu items

My menu is made up of ul/li items with background images, but I'm having trouble adjusting the opacity and alignment of the images. While there are resources online for changing background image opacity, such as this article: https://scotch.io/tutoria ...

Display or conceal div elements using JavaScript

Is there a way to hide certain divs when clicking on a specific div box? Clicking on the box will trigger the hiding of some application divs. See the first image below: When the boxes are hidden, the other divs will readjust in place of the current divs ...

Display a preloader image while waiting for the content to load using jQuery or AJAX

Hey there, I could really use some help with a little issue. I've been trying to use the click() and load() functions to bring my content into a specific CSS class. $("#feed").click(function(){ $(".homefeed").load("feedcontainer.php"); ...

What is the best way to insert a space between my element and pseudo-element?

I'm currently utilizing the ::before selector to insert an image before each item on my menu list. I am aiming to include a blank space between the image and the text. Below is the HTML markup <ul> <li>menu 1</li> <li>me ...

Using CSS transition on the height property does not produce the desired effect

I'm currently working on a menu interaction that activates on click. Here is the code snippet for reference: let btn = document.querySelector('.trigger'); let icons = document.querySelector('.icons'); let labels = document.query ...

Position a single sizable DIV in line with two smaller ones

I'm attempting to create a layout similar to this: Here is the code snippet I am using: !-- BIG ONE LEFT --> <div class="container"> <div class="row"> <div class="col-lg-8"> <div class="row"> ...

What could be causing the header's height to remain unchanged when using a percentage for its size?

I'm having trouble creating a header that takes up 20% to 40% of the body. When I try using percentages, it doesn't work but it works fine with pixels. Can someone explain why percentages aren't working for this? *{ margin:0; padding: ...

Adjust the color of the contact information when the hamburger menu is activated

I'm currently working on customizing my WordPress site, which uses the Enfold theme. I have implemented a burger menu for all screen widths, and now I want to change the color of the contact information (phone and email) when the burger menu is opened ...

Which is better for website footers: tables or nested ULs?

I am trying to create a footer with a specific layout that consists of item titles and images. The design I have in mind includes three columns, where the middle one is larger than the sides. The middle column should be 550px wide, while the side columns s ...

Unable to display background image and color on Internet Explorer

The background beneath the slider and footer appears normal in most browsers, but there seems to be an issue with Internet Explorer. Check it out at this link: Do you have any suggestions on how to fix this? ...

Navigating the art of employing different fonts for a website

I am looking to incorporate the trainway font into my website, but I'm concerned that the user may not have it installed on their device. Is there a way to showcase text in a specific font without requiring the user to download and install it? Thank ...

Bringing the Jquery cursor into focus following the addition of an emoji

Looking to add emojis in a contenteditable div but facing an issue with the cursor placement. Here is a DEMO created on codepen.io. The demo includes a tree emoji example where clicking on the emoji adds it to the #text contenteditable div. However, after ...

Achieve smooth scaling of font size in CSS according to body width without the need for JavaScript

Can the font size be smoothly scaled in relation to the width of the body or parent element? I am seeking a solution that does not involve javascript. Is it feasible to relate rem to a specific width? ...

Is Bootstrap 4.0.0 not compliant with W3C standards?

I'm struggling with an issue in my application where I am attempting to minimize all of my .css files. It appears that Bootstrap is not passing W3C validation, both in the regular and .min versions. Any insights on how to resolve this issue? ...

Scrolling Vertically using WinJS ListView

Is it possible to achieve vertical scrolling in a ListView using HTML/JS for Windows 8 applications with WinJS? I successfully created a Windows 8 app with XAML/C# that utilizes a ListView for vertical scrolling. However, when attempting to replicate the ...

Tips for modifying the appearance of a text file within an iframe

Lately, I've been facing a dilemma on my website. I have an iframe code that looks like this: <iframe src ="http://hokuco.com/cde/user.txt" id ="myIframe"></iframe> However, the consolas font used in the iframe makes my website appear ...