When Ruby on Rails is combined with HTML and CSS and marked as selected, it will be displayed with

I'm currently in the process of developing my cookbook app and the next feature on my agenda is the grocery list functionality. Once on the recipe page, users will have the option to click on an "Add to Grocery List" link which will redirect them to a page containing all the ingredients needed for that particular recipe.

The idea is to display the ingredients individually, each with a checkbox next to it. My goal is to have a strikethrough effect applied to the ingredient text when the checkbox is ticked (Eggs), and for the text to remain normal when the checkbox is unticked. This is where I could really use some assistance. I understand that I will need to use If statements, but I'm unsure of how to implement them while achieving the desired outcome.

Currently, I have the ingredients stored using the text_area method, meaning that in order to distinguish between ingredients, the each_line function must be utilized.

Here is a snippet from the _grocery partial that I have constructed:

<% @recipe.ingredient.each_line do |line| %>
  <li><%= line %></li>
<% end %>

Since I am not using bootstrap, I am unsure of how to incorporate the checkbox and apply the strikethrough effect when the checkbox is selected. Any guidance or insights would be greatly appreciated. If additional code is required from my end, please do not hesitate to request it.

This particular aspect of the project has me feeling a bit out of my depth.

Answer №1

To apply a strike-through effect using only CSS is possible. To ensure the item's state is saved, it is recommended to utilize AJAX for automatic form saving. Alternatively, including a "Save List" button can mimic a typical form setup in Rails.

Here is a demonstration of using CSS to dynamically strike through an item when the checkbox is checked, or upon loading the page with the checkbox already checked.

.row input:checked + span {text-decoration: line-through}
<div class="row"><input type="checkbox"> <span>My item</span></div>
<div class="row"><input type="checkbox"> <span>My item</span></div>
<div class="row"><input type="checkbox"> <span>My item</span></div>
<div class="row"><input type="checkbox"> <span>My item</span></div>
<div class="row"><input type="checkbox"> <span>My item</span></div>

Upon page load, utilize Ruby to determine if the checkbox should be pre-checked, easily achieved with Rails' form helper. Let CSS handle the visual component from there :)

Answer №2

In addition to the fantastic CSS-only solution provided by Wes Foster, I'll get straight to the point for you...


It's important to note that your question pertains to front-end styling, not Rails.

While the information you've given is helpful, it appears that there might be some confusion regarding how the system should be integrated.


I have set up ingredients using text_area

Here is some code (the aforementioned approach may not be ideal).

Refer to Wes's solution as it will enhance the efficiency of your task:

#app/models/ingredient.rb
class Ingredient < ActiveRecord::Base
   has_and_belongs_to_many :recipes
end

# join table - ingredients_recipes - ingredient_id | recipe_id

#app/models/recipe.rb
class Recipe < ActiveRecord::Base
   has_and_belongs_to_many :ingredients
end

One of the main issues in your structure is manually adding "ingredients" to a recipe. It is strongly recommended to utilize a many-to-many relationship (either has_and_belongs_to_many or has_many :through) to link different records together.

In essence, this will enable you to implement the following:

@recipe.ingredients.each ...

#app/controllers/recipes_controller.rb
class RecipesController < ApplicationController
   def show
      @recipe = Recipe.find params[:id]
   end
end

#app/views/recipes/show.html.erb
<% @recipe.ingredients.each do |ingredient| %>
    <%= content_tag :li, ingredient, class: "ingredient" %>
<% end %>

#app/assets/stylesheets/application.css
li.ingredient:checked + span {  /* Wes's code */ }

This will display a list of ingredients for your recipe.


If you wish to add or remove ingredients from a recipe:

#app/models/recipe.rb
class Recipe < ActiveRecord::Base
   has_and_belongs_to_many :ingredients
   accepts_nested_attributes_for :ingredients
end

#app/controllers/recipes_controller.rb
class RecipesController < ApplicationController
   def new
      @recipe = Recipe.new
   end

   def create
      @recipe = Recipe.new recipe_params
      @recipe.save
   end

   def edit
     @recipe = Recipe.find params[:id]
   end

   def update
      @recipe = Recipe.find params[:id]
      @recipe.update recipe_params
   end

   private

   def recipe_params
      params.require(:recipe).permit(:recipe, :params, :ingredients)
   end
end

This will allow you to do the following:

#app/views/recipes/edit.html.erb
<%= form_for @recipe, method: :put do |f| %>
   <%= f.collection_check_boxes :ingredients, @recipe.ingredients, :id, :name %>
   <%= f.submit %>
<% end %>

With this setup, you can view the existing ingredients for your @recipe and update them upon submission.

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

Once the 'add to cart' button is selected, it must remain disabled until the product is deleted from the user's cart page

Embarking on my inaugural web development project, I am utilizing PHP, AJAX, and jQuery. I seek a resolution for the following challenge: Upon a user clicking the 'add to cart' button, that particular product or the button itself should become d ...

What are the steps for transitioning with JavaScript?

My goal is to make both the <hr> elements transition, but I'm struggling with only being able to select the lower <hr> using CSS. html { margin-bottom: 0; height: 100%; min-height: 100%; } body { margin-top: 0; height: 100%; ...

Creating a static Top Bar that remains unaffected by page refreshing using Ajax or any other method can be achieved by implementing JavaScript and CSS

I've designed a sleek top bar for my upcoming website project. Below is the CSS code snippet for creating this clean div bar: .topbar { display:block; width:100%; height:40px; background-color:#f5f5f5; } I'm looking to incorporate a simple .SWF ...

The footer has a wandering nature, constantly moving around as the wrapper mysteriously vanishes

I understand that this question has been asked multiple times before, but I am struggling to get it right! My goal is to keep the footer at the bottom of the page, but it seems to be acting strangely. Here is my HTML: <!DOCTYPE HTML> <html& ...

Managing dynamically appearing elements in Ember: Executing a Javascript function on them

I'm currently working on a project in Ember and facing an issue with calling a JavaScript function when new HTML tags are inserted into the DOM after clicking a button. Below is a snippet of my code: <script type="text/x-handlebars" id="doc"&g ...

What is the best way to embed a video and playlist onto a webpage using HTML5?

After switching from the flash-based JWPlayer 4 to JWPlayer 5 with HTML5 support, I noticed that while the player degrades gracefully on mobile devices without Flash but with HTML5 support, it breaks when the playlist option is enabled. Can someone please ...

When making an ajax request in Codeigniter, the form validation may return false

Greetings, I am in the process of creating a website using CI on Backend and I'm facing an issue with implementing a registration system without refreshing the page. When I use a post form submit, everything works fine with no errors. However, when at ...

MDL Tab loading Problem

When visiting my website at this link, which is powered by MDL from Google, there is a troublesome issue that occurs. The #harule tab briefly appears before disappearing. This tab should actually be hidden on the page until a user clicks on the harule tab ...

Retrieving the current element in the .each() method

Here is some code that I am working with: HTML <div id="step-holder"> <div class="step-no">1</div> <div class="step-dark-left"><a href="#">Basic details</a></div> <div class="step-dark-right">&a ...

What is the method for linking an icon in an iconfont using a keyword?

For my project, I would like to incorporate icon fonts and customize the font file using FontForge. I understand that glyphs can be referenced by unicode in HTML or CSS, for example <i>&#xe030;</i> and &::before{content: "\e030";} ...

The Issue of Missing HTML Registration Form Data in Django

My configurations in demo\urls.py, user\urls.py, views.py, and HTML form seem to be set up correctly. However, upon pressing the submit button on the HTML form, I encounter a "File Not Found" error. Even after seeking assistance from ChatGPT, fol ...

Integrate, Delay, Experimentalize, and Attach components

This inquiry might lean more towards a general browser/javascript discussion rather than a focused prototype question, but I believe this community possesses a deep understanding of javascript and browsers. With that said, here is my query: If the followi ...

Transforming an HTML file into a Vue.js component

I'm working on a login.vue file and I need to incorporate styling along with the necessary js and css files. Unfortunately, I'm clueless about how to achieve this within the login.vue file. Below is a snippet from the html file: <!DOCTYPE ht ...

How can we enhance our proxyURL in Kendo UI with request parameters?

As outlined in the Kendo UI API documentation, when using pdf.proxyURL with kendo.ui.Grid, a request will be sent containing the following parameters: contentType: Specifies the MIME type of the file base64: Contains the base-64 encoded file content fil ...

Webpack fails to handle CSS background images

I'm having trouble with my Webpack configuration as it's not processing CSS images set in the background property: background: url('./hero.jpg') no-repeat right; This is resulting in an error message that reads: ERROR in ./src/app/comp ...

Converting Minecraft JSON API values to percentages, then translating them to width dimensions

I need to retrieve details for a Minecraft server using an API system (from ). I want to display the "online players" and "max players" values, calculate a percentage, and use that to fill the width of a progress bar. However, when implementing this on En ...

Placing two images within one div tag

I've been attempting to place two images within a single div, but they're not appearing how I intended. I'd like there to be some space between the images, but that's not happening. Here's the CSS I'm using: .sidebarBody { ...

Having trouble passing data between view controllers

In my AngularJS application, I have a table column in main.html that is clickable. When clicked, it should redirect to a new customer page with the value of the column cell as the customer's name. There is a corresponding service defined for the app m ...

Dealing with form submission issues in jQuery and jqGrids

Lately, I've been experimenting a lot with jqgrids and have almost everything set up the way I want it - from display to tabs with different grids. Now, I'm trying to utilize Modals for adding and editing elements on my grid. The issue I'm ...

Controlling Material-UI using a custom .css stylesheet

Are there alternative methods for customizing Material-UI components using CSS stylesheets? I'm aware of the {makeStyles} method and JSS overrides, but I find it messy in the code and confusing when trying to modularize it in other files. Is there a ...