lundi 18 juin 2018

Rails: Select items from a db and insert them into another db (2 different controllers)

I have a list displayed from a model distrib. And I would like the user to be able to select items in the list and add all of them to a table called listdedistribution (sorry for the frenglish). I manage to get the check-box displayed but I get stucked there. I would also like to paginate if that is possible, without forgetting the selection from the first page. My distrib/index.html.erb :

<%= form_tag listdedistributions_path %>
    <table class="table table-hover">
      <thead>
      <th scope="col"> Id</th>
      <th scope="col"> First Name</th>
      <th scope="col"> Last Name</th>
      <th scope="col"> Email</th>
      <th scope="col"> Company</th>
      <th scope="col"> Select</th>
      <th scope="col"> Ind. Delete</th>

      </thead>
      <% @distribs.each do |distrib| %>
        <tbody>
        <tr>
          <th scope="row">   <%= distrib.id %> </th>
          <td>    <%= best_in_place distrib, :first_name %>
          </td>
          <td>    <%= best_in_place distrib, :last_name %>
          </td>
          <td>    <%= best_in_place distrib, :email %>
          </td>
          <td>    <%= best_in_place distrib, :company %>
          </td>
          <td>
            <%= check_box("distribs_selected", "selected_distribs[]", {multiple: true}, distrib.id, nil) %>
          </td>
          <td>
            <%= link_to (t :Delete), distrib_path(distrib), method: :delete, data: {confim: "You will be deleting this item, Are you sure ?"}, class: "btn btn-default" %>
          </td>
        </tr>
        </tbody>
      <% end %>
    </table>
    <%= submit_tag "Add to mailist" %>
  </div>

My controller for distrib:

class DistribsController < ApplicationController

  before_action :find_distrib, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :authenticate_user!
  respond_to :html, :json

  def index
    @distribs = Distrib.all.order("created_at DESC")
  end
  def show
    @distribs = Distrib.all.order("created_at DESC")
  end
  def new
    @distrib = current_user.distribs.build
  end
  def edit
  end
  def destroy
    @distrib = Distrib.find params[:id]
    if @distrib.present?
      RemovedContact.create(:email => @distrib.email, :user_id => @distrib.user_id, :first_name => @distrib.first_name, :last_name => @distrib.last_name, :company => @distrib.company,)
      @distrib.delete
      redirect_to distribs_path
    else
      redirect_to distribs_path
    end
  end
  def distrib_params
    params.require(:distrib).permit(:first_name, :last_name, :email, :user_id, :telephone, :company, :address, :city, :country, :memo, :location_met)
  end
  def update
    @distrib = Distrib.find params[:id]
    respond_to do |format|
      if @distrib.update(distrib_params)
        format.html {redirect_to(@distrib, :notice => 'Updated was successfull.')}
        format.json {respond_with_bip(@distrib)}
      else
        format.html {render :action => "edit"}
        format.json {respond_with_bip(@distrib)}
      end
    end
  end
  def create
    @distrib = current_user.distribs.build(distrib_params)
    if @distrib.save
      redirect_to distribs_path
    else
      render 'new'
    end
  end
  def listdedistribution
    @listdedistribution = current_user.listdedistributions.build
  end
  def find_distrib
    @distrib = Distrib.find(params[:id])
  end
  def make_inactive
    @distrib = Distrib.find params[:id]
    @distrib.update_columns(activecontact: "1")
  end
  def make_active
    @distrib = Distrib.find params[:id]
    @distrib.update_columns(activecontact: "0")
  end
end

My Listdedistrution controller

class ListdedistributionsController < ApplicationController
  before_action :find_listdedistribution, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :authenticate_user!


  def index
    @listdedistributions = Listdedistribution.all.order("created_at DESC")
  end

  def show
    @listdedistributions = Listdedistribution.all.order("created_at DESC")
  end

  def new
    @listdedistribution = current_user.listdedistributions.build
  end


  def edit
  end

  def destroy
    @listdedistribution.destroy
    redirect_to listdedistributions_path
  end

  def update
    if @listdedistribution.update(listdedistribution_params)
      redirect_to listdedistributions_path(@listdedistribution)
    else
      render 'edit'
    end
  end

  def create
    @listdedistribution = current_user.listdedistributions.build(listdedistribution_params)

    if @listdedistribution.save

      redirect_to listdedistributions_path
    else
      render 'new'
    end
  end

  def listdedistribution_params
    params.require(:listdedistribution).permit(:distrib_id, :name, :user_id, :group_id)
  end

  def find_listdedistribution
    @listdedistribution = Listdedistribution.find(params[:id])
  end

  def find_sender
    @listdedistribution = Listdedistribution.find(params[:user_id.email])
  end

  def find_entreprise
    @listdedistribution = Listdedistribution.find(params[:entreprise_id])
  end
end

my Listdedistribution schema looks like:

  create_table "listdedistributions", force: :cascade do |t|
    t.integer "distrib_id"
    t.integer "user_id"
    t.integer "group_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

I do not think that it is complex, but do not have an idea of how to head next. Can someone help please? Thank you

Aucun commentaire:

Enregistrer un commentaire