Altering element content based on the current database record in Ruby on Rails

Take a look at the image below to get an idea of how it should appear.

In my Ruby on Rails database, I have a table named sauces.

Here are the fields in the sauces table:

  • id
  • name
  • description
  • picture

This is the CSS code:

#container{width:950px;height:800px;}
#title{this_is:irrelevant;}
#sauce_picture{bla bla}
#sauce_name{bla bla}
#sauce_splatter{background-image:url("someimage.png")}

HTML Code:

<html>
 <body>
  <div id="container>
   <div id="title><h1>SAUCES</h1></div>
   <div id="sauce_picture><img src="somepic.png/></div>
   <div id="sauce_name><h2>Sauce 1</h2></div>
   <div id="sauce_splatter>
     <p>Sauce Description Goes Here</p> 
   </div>
  </div>
 </body>
</html>

I have 6 records in the Rails database table and each field is filled. When the page loads, I want the content to display the information from record number one (<img src="somepic.png/>, <h2>Sauce 1</h2>,

<p>Sauce Description Goes Here</p>
) because the count starts at one.

I'm not sure how to set it up so that when the right arrow key is pressed, the id increments to two and displays the details of record number 2 (id=2).

Lastly, I need to figure out how to disable the left arrow link when id=1 (first record) and disable the right arrow link when the last record is reached. This functionality can be achieved through changing elements with jQuery, but integrating Rails and jQuery is something I need guidance on.

This approach will eliminate the need to modify any code when adding new sauces to the database table in the future, which aligns perfectly with MVC principles.

Thank you!

Answer №1

One way to tackle this issue is by passing an array like the following:

@sauces = Sauce.all

In your view, you can initially render one entry from the array and incorporate some JavaScript logic to handle updating the divs based on the selected sauce. This approach works well for smaller Sauces-Tables with fewer entries.

For larger tables, utilizing AJAX to fetch the selected sauce from the backend is recommended. However, you will still need JavaScript to update the content divs once the asynchronous call completes.

Another option is to render a div for each sauce entry but only display the selected one. By clicking a button, the hidden div containing the chosen sauce will be shown while hiding the others using JavaScript.

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

Tips for properly filling empty spaces in a flexbox layout using HTML

Is there a way to fill in the gap using CSS so that boxes automatically take up the empty spaces, even if they are of different sizes? How can I make sure the boxes adjust automatically as the screen size changes? .c1 { display: flex; flex-wrap: w ...

The JQuery functions for hiding and showing elements are not being implemented

Having issues with Jquery's .show and .hide functions. I've tried various solutions but can't seem to pinpoint the problem. Any assistance would be greatly appreciated. <div class="splash-page" style="overflow: hidden; display: f ...

Alter the size of HTML text using a button command

Hello everyone! I recently developed a basic app that showcases an HTML file in a web view. The HTML file is located in the main bundle. I am seeking advice on how to implement functionality that allows users to adjust the font size of the HTML file using ...

Refreshing the <select> value following changes to <option> tags

I've included my code snippet below. The goal is to update the options of a select box and then reset its value back to what it was before the change. Unfortunately, this approach is not achieving the desired result. Any insights on what might be caus ...

Failure to recognize AJAX content in .ready function

When using an AJAX call to load content, primarily images, I encountered a challenge. I wanted the div to fade in only after all the images were fully loaded. To achieve this, I made use of .ready method in jQuery. However, it seemed like the images were n ...

Create a dialog box with rounded triangle corners

In the process of developing an application, we are incorporating a text display feature within a div that resembles a chat box direction, but with a curved style as depicted in the screenshot linked below. Desired design exemplified in purple Target des ...

What are some ways to transfer data between the JavaScript code of a main webpage and a pop-up window?

Snippet from my main page JavaScript file: var PopupConnect = window.open("http://localhost:8080"); var popup_function = PopupConnect.CreateText(); Script from the popup page JavaScript file: function CreateText() { var text = "random text"; ...

I am looking to modify various attributes linked to Rails

My Desire for Reality I am looking to update multiple related values in Rails by sending an update request from JavaScript. While creating data was seamless, I encountered difficulties when attempting to update it. #Code JavaScript* export const actions ...

Leveraging Jquery Splice

Trying to eliminate an element from an array using the Splice method. arrayFinalChartData =[{"id":"rootDiv","Project":"My Project","parentid":"origin"},{"1":"2","id":"e21c586d-654f-4308-8636-103e19c4d0bb","parentid":"rootDiv"},{"3":"4","id":"deca843f-9a72 ...

Circular arrangement using D3 Circle Pack Layout in a horizontal orientation

I'm currently experimenting with creating a wordcloud using the D3 pack layout in a horizontal format. Instead of restricting the width, I am limiting the height for my layout. The pack layout automatically arranges the circles with the largest one ...

Utilizing the search feature with jQuery UI autocomplete: a step-by-step guide

I'm in the process of creating a website that utilizes a search bar with jQuery UI autocomplete plugin. Currently, the search functionality only works when pressing Enter on the keyboard or clicking the mouse after selecting an option from the dropdo ...

Generate numerous div elements by utilizing ng-repeat

I am trying to generate 'n' red blocks with text (where n represents the number of elements in an array), but unfortunately, I am seeing a blank page. <html> <body> <script src="https://ajax.googleapis.com/ajax/libs/angu ...

Using $_GET is not allowed when using jQuery.load to load a file

Trying my hand at creating a team chat using jQuery and PHP (totally new to jQuery) and I've almost got it working flawlessly, except for one tiny hiccup. I've split the code into 4 files: teams.php (contains the $_GET information) teamchat.js ...

Tips for invoking a function in a div element without relying on *ngIf in Angular 2

When filtering JSON data in a nested loop, I have filters set up in the child loop using ngFor. However, I also need to apply that filter at the parent level in Angular 2. Currently, I am using nglf to call the function and it is working but I am looking ...

What methods can be used to retrieve text data from a website built with Angular technology?

Exploring new territories with Angular and Selenium, I find myself facing a challenge in extracting specific text fields from a website. The precise value seems elusive within the intricate web of HTML code. Seeking guidance or tips from experienced indivi ...

What is the best way to add the ".html" suffix to pages in nuxt.js?

Is it possible to add the ".html" extension to a page address? For instance, changing "test_page" to "test_page.html" ...

maximum number of results in google custom search limit

I'm trying to retrieve the top 40 results from the Google API, but when I limit the result using the code below, it doesn't seem to work. How can I achieve getting the top 40 results with the Google API? <script> (function() { ...

Exploring the wonders of CSS with Socialchef

Hey, I recently came across a solution on Stack Overflow where they provided code for SocialChef to convert decimal inputs into fractions. I'm wondering how I can write or incorporate some code to input fractions and mantissa as a fraction... I ...

What causes a div with an image inside to not expand the height of its parent div?

I'm puzzled as to why the mainCountainerHeadLogo isn't expanding the parent div, mainCountainerHead, in terms of height. Interestingly, when I adjust the page scale, both mainCountainerHeadTitle and mainCountainerHeadMenu do expand the mainCount ...

Overlaying images on top of text

Is there a way to overlay an image on text, like a pattern image? Similar to applying color with a hex value in CSS: color: #ffffff; Any ideas on how this can be achieved? I want to have a pattern over text. https://i.sstatic.net/JaNZU.jpg Appreciate ...