Creating a List or Array of Objects in Ruby on Rails

I am trying to provide users with a created web application for creating lists of objects they created.

For example, the user has a list of objects, such as products, which can range from apples to oranges to pop cakes.

I would like for me to be able to return all products added to the database by the user and create a list by selecting those that should be in their product list.

Preferably, it would be styled so that they can click the checkboxes for the ones they wanted, and then click Save to create a new list.

I looked at the relations own_to, has_many and tried to create a list object that has many products, but I can not understand part of the form of this strategy. I would appreciate any / all advice. Thank!

Here is the code that I have at the moment, I initially omitted it because I don't think I'm on the right track, but here it is anyway / just in case / provides more context:

Grocery Model:

class Item < ApplicationRecord
    belongs_to :list, optional: true
end

Model List

class List < ApplicationRecord
    has_many :items
end

List controller

class ListsController < ApplicationController
  before_action :authenticate_user!
  layout 'backend'

  def index
    @lists = List.where(user_id: current_user.id)
  end

  def show
  end

  def new
    @list = List.new
  end

  def edit
  end

  def create
    @list = List.new(list_params)
    @list.user = current_user

    if @list.save
      redirect_to list_path(@list.id), notice: 'List was successfully created.'
    else
      redirect_to list_path(@list.id), notice: 'List was not created.'
    end
  end

  def update
    respond_to do |format|
      if @list.update(list_params)
        format.html { redirect_to @list, notice: 'List was successfully updated.' }
        format.json { render :show, status: :ok, location: @list }
      else
        format.html { render :edit }
        format.json { render json: @list.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @list.destroy
    respond_to do |format|
      format.html { redirect_to lists_url, notice: 'List was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Never trust parameters from the scary internet, only allow the white list through.
    def list_params
      params.require(:list).permit(:name, :items)
    end
end

Not sure what to do with the form - tried something like http://apidock.com/rails/ActionView/Helpers/FormHelper/check_box

+4
source share
2 answers

I would solve this by implementing a third model that maintains a relationship between products and a list. And then you can process it in forms with :accepts_nested_attributes_for.

To give an example, here is how I would structure the models:

class List < ApplicationRecord
  has_many :list_items,   inverse_of: :list
  has_many :items,        through: :list_items

  # This allows ListItems to be created at the same time as the List, 
  # but will only create it if the :item_id attribute is present
  accepts_nested_attributes_for :list_items, reject_if: proc { |attr| attr[:item_id].blank? }
end

class Item < ApplicationRecord
  has_many :list_items
  has_many :lists,        through: :list_items
end

class ListItem < ApplicationRecord
  belongs_to :list, inverse_of: :list_items
  belongs_to :item
end

, .

<h1>New List</h1>
<%= form_for @list do |f| %>
  <% @items.each_with_index do |item, i| %>
    <%= f.fields_for :list_items, ListItem.new, child_index: i do |list_item_form| %>
      <p>
        <%= list_item_form.check_box :item_id, {}, item.id, "" %> <%= item.name %>
      </p>
    <% end %>
  <% end %>
  <p>
    <%= f.submit 'Create List' %>
  </p>
<% end %>

, , @items - , , . , FormBuilder fields_for.

, :child_index , (.. name="list[list_item_attributes][0][item_id]") , , .

FormBuilder check_box :

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")

, , , , item.id, , . accepts_nested_attributes_for List, , , :item_id , ListItems .

, , , :

def allowed_params
  params.require(:list).permit(list_items_attributes: [:item_id])
end
+2

, , , .

<% form_for @list do |f| %>
  <% Item.all.each do |item|
    <%= f.check_box(:items, { :multiple => true }, item.id) %>
  <% end %>
<% end %>

, list_id. , "list_id" Item, , , .

?

0

Source: https://habr.com/ru/post/1666335/


All Articles