Get and display all values from database row separately based on select (Rails)

送分小仙女□ 提交于 2020-01-25 20:30:34

问题


Hi i'm stuck making an invoicing application. I want to have an option where you can add an existing customer from the customers table and add those values to the invoice.

The values i want to display are company_name, vat_number and iban_number.

I tried doing this:

<%= select_tag 'choose customer', options_from_collection_for_select(current_user.customers.all, 'id', 'company_name') %>

But obviously this only gets the value of company_name.

I tried using collection.select but that one also gets only one value of the database row instead of all 3.


I want to be able to select from a list or table row containing just the company_name but when i click on that company_name it has to also display vat_number and iban_number(separately, on different parts of the page).

something like this in my invoice/_form would be optimal:

<p><%= customer.selected(:company_name) %></p>
<p><%= customer.selected(:vat_number) %></p>
<p><%= customer.selected(:iban_number) %></p>

This is my invoices controller:

class InvoicesController < ApplicationController
  before_action :set_invoice, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  # GET /invoices
  # GET /invoices.json
  def index
    @invoices =  current_user.invoices.all
    flash.now[:notice] = "U haven't added any invoices yet" if @invoices.empty?
  end

  # GET /invoices/1
  # GET /invoices/1.json
  def show
  end

  # GET /invoices/new
  def new
    @invoice = current_user.invoices.new
    @invoice.build_company
    @invoice.products.build
    @invoice.build_customer
  end

  # GET /invoices/1/edit
  def edit
  end

  # POST /invoices
  # POST /invoices.json
  def create
    @invoice = current_user.invoices.new(invoice_params)

    respond_to do |format|
      if @invoice.save
        format.html { redirect_to @invoice, notice: 'Your invoice is saved.' }
        format.json { render :show, status: :created, location: @invoice }
      else
        format.html { @invoice.build_company
                      @invoice.products.build
                      @invoice.build_customer
                      render :new }
        format.json { render json: @invoice.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /invoices/1
  # PATCH/PUT /invoices/1.json
  def update
    respond_to do |format|
      if @invoice.update(invoice_params)
        format.html { redirect_to @invoice, notice: 'Your invoice is edited.' }
        format.json { render :show, status: :ok, location: @invoice }
      else
        format.html { render :edit }
        format.json { render json: @invoice.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /invoices/1
  # DELETE /invoices/1.json
  def destroy
    @invoice.destroy
    respond_to do |format|
      format.html { redirect_to invoices_url, notice: 'Your invoice is deleted.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_invoice
      @invoice = current_user.invoices.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def invoice_params
      params.require(:invoice).permit(:number, :currency, :date, :duedate, :btwtotal, :subtotal, :total, :footer,
                                      customer_attributes: [:id, :company_name, :address_line_1, :iban_number, :vat_number, :zip_code, :_destroy],
                                      company_attributes: [:id, :iban_number, :company_name, :_destroy],
                                      products_attributes: [:id, :quantity, :description, :unitprice, :btw, :total])
    end
end

The select dropdown is inside of the new action and the selected data should be visible in the new action, index action and show action and editable in the edit action.


I read the entire ruby docs and they are very vague about this particular instance.

Note: i don't want just an answer, i want to be able to understand how to do it in future projects with different criteria. And i want other people to understand the underlying concepts aswell.

Any ideas on how to achieve this or where to look for the answer would be much much appreciated


回答1:


<%= select_tag 'choose customer', options_from_collection_for_select(current_user.customers.all, 'id', 'company_name'), id: "choose_customer" %>

You can bind the change event from the rails to an ajax call, send the id of the selected object, if you want to fetch the data based on the selected value, call a method, then handle the view using that response

    $(document).ready(function() { 
    $("#choose_company").bind("change",function() { 
        if ($(this).val() != undefined) { 
            $.ajax({ 
                url : "/my_action", 
                data: {'company': $(this).val()}, 
                dataType: "json", 
                type: "GET", 
                success : function(data) { 
                    $(".display_btw").html(data["btw_number"]); 
                    $(".display_iban").html(data["iban_number"]); 
                    $('.chosen_company_btw').val(data["btw_number"]).text(data["btw_number"]); 
                    $('.chosen_company_iban').val(data["iban_number"]).text(data["iban_number"]); 
                } 
            }) 
        } 
    }) 
})

Your controller action can be something like,

get '/my_action', to: 'invoices#my_action' add this route.

def my_action 
    if params[:company].present? 
        @company = current_user.companies.find(params[:company]) 
        @data = Hash.new 
        @data["btw_number"] = @company.btw_number 
        @data["iban_number"] = @company.iban_number 
        render json: @data and return false 
    end 
end

This action returns the vat_number and iban_number which should be displayed in the view depending in the selected value.




回答2:


Create a method on the customers model that concatenates this information together in a friendly, human-readable way:

def friendly_info
  "#{company_name} (#{vat_number}) - IBAN #{iban_number}"
end

Then use that method in your select statement in place of company_name.



来源:https://stackoverflow.com/questions/40061008/get-and-display-all-values-from-database-row-separately-based-on-select-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!