jeudi 11 avril 2019

Rails, need to update empty model via checkboxes

I'm new to Ruby on Rails and am working on a project with several people that allows users to select a recipe via checkboxes which will then show the individual ingredients from the recipes chosen on the next page. We are working with two controllers (Recipes and Baskets), and their respective views and models. The ActiveRecord for Recipes is populated with recipe_name and recipe_ingredient_array which holds many ingredients. The basket model is empty, and needs to take the recipe_ingredient_array items and populate the empty basket model via checkboxes the user selects. The models for both recipes and baskets has a has_many_and_belongs_to relation.

This is my first post so I am sorry if I went about asking this question poorly.

I have tried several different paths to fix this problem, but have not been able to find a fix. As I am working on this with several people who are new to rails, I believe we may have caused this issue to be much harder due to the fact we do not have any forms or form helpers, and the views were generated completely through HTML (I could be wrong in this).

I have researched forms, form helpers, check_box_tags, etc but am not sure how to implement them into our current structure.

Recipes controller:

class RecipesController < ApplicationController
    def allRecipes
        @recipes = Recipe.all
    end
    def create
        @recipe = Recipe.new(recipe_params)

        @recipe.save
        redirect_to @recipe
    end

    def destroy
        @recipe = Recipe.find(params[:id])
        @recipe.destroy

        redirect_to recipes_path
    end
end

private
    def recipe_params
        params.require(:recipe).permit(:recipe_name, :recipe_ingredient_array)
    end

Baskets controller:

class BasketsController < ApplicationController

    def basket
       # @baskets = Basket.all
    end

    def calculation
        #will be implemented when basket active record is populated
    end

    def create
        @basket = Basket.new(basket_params)

        @basket.save
        redirect_to @basket
    end

    def destroy
        @basket = Basket.find(params[:id])
        @basket.destroy

        redirect_to recipes_path
    end
end

private
    def basket_params
        params.require(:basket).permit(:ingredient_name, :ingredient_count, :ingredient_organic)
    end

Example of an item in Recipe active record:

Recipe id: 1, recipe_name: "recipe_39", recipe_ingredient_array: ["ingredient_146", "ingredient_141", "ingredient_043", "ingredient_162", "ingredient_066", "ingredient_036", "ingredient_066", "ingredient_062"]

allRecipes.html.erb

<h1>Available Recipes</h1>
<div id="recipes-table">
    <table>
        <tr>
            <th>Name</th>
            <th>Ingredients</th>
            <th>Select</th>
        </tr>
        <% @recipes.each do |recipe| %>
           <tr>
               <td><%= recipe.recipe_name %></td>
               <td><%= recipe.recipe_ingredient_array %></td>
               <td><input type="checkbox" name="select-recipe" /></td>
           </tr>
        <% end %>
    </table>
</div>
<br/>
<div class="next-step-btn">
        <%= link_to "Add Recipe(s) to Basket", basket_path, class: "button" %>
    </div>


basket.html.erb

<h1>My Basket</h1>

<div id="basket-table">
    <table>
        <tr>
            <th>Item</th>
            <th>Count</th>
            <th>Organic?</th>
        </tr>
        <% if false %>
         <% @baskets.each do |basket| %>
           <tr>
               <td><%= basket.ingredient_name %></td>
               <td><%= basket.ingredient_count %></td>
               <td><input type="checkbox" name="select-organic" /></td>
           </tr>
        <% end %>
        <% end %>
    </table>
</div>
<br/>
<div class="next-step-btn">
    <%= link_to "Calculate My Budget", calculation_path, class: "button" %>
</div>




Aucun commentaire:

Enregistrer un commentaire