【问题标题】:Get and display all values from database row separately based on select (Rails)根据选择(Rails)分别获取和显示数据库行中的所有值
【发布时间】:2017-02-24 22:32:29
【问题描述】:

您好,我无法制作发票申请。我想要一个选项,您可以从customers 表中添加现有客户并将这些值添加到发票中。

我要显示的值是company_name、vat_number 和iban_number。

我试过这样做:

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

但显然这只会得到company_name的值。

我尝试使用collection.select,但它也只获得数据库行的一个值,而不是全部 3 个。


我希望能够从仅包含 company_name 的列表或表格行中进行选择,但是当我单击该 company_name 时,它​​还必须显示 vat_number 和 iban_number(分别在不同的部分页)。

在我的发票/_form 中这样的内容是最佳选择:

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

这是我的发票控制器:

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

选择下拉菜单位于新操作中,所选数据应在新操作、索引操作和显示操作中可见,并且在编辑操作中可编辑。


我阅读了整个 ruby​​ 文档,但他们对这个特定实例非常模糊。

注意: 我不想要一个答案,我希望能够了解如何在未来具有不同标准的项目中做到这一点。我希望其他人也能理解基本概念。

任何关于如何实现这一点或在哪里寻找答案的想法都将非常感激

【问题讨论】:

  • vat_number 和 iban_number 是公司特有的吗?
  • 他们属于客户,我不小心将客户切换到 options_from_collection_for_select 中的公司。已编辑。
  • 如何在控制器中将这些值作为数组获取并在视图中使用它们?
  • 但是如何将它与一个选择选项结合起来,您可以从一个数据属性中进行选择,并在页面的不同部分和所有控制器操作视图中分别显示所有 3 个数据属性。

标签: javascript ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4


【解决方案1】:
<%= select_tag 'choose customer', options_from_collection_for_select(current_user.customers.all, 'id', 'company_name'), id: "choose_customer" %>

您可以将rails中的更改事件绑定到ajax调用,发送所选对象的id,如果您想根据所选值获取数据call a method,然后使用它处理视图回应

    $(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"]); 
                } 
            }) 
        } 
    }) 
})

您的控制器操作可能类似于,

get '/my_action', to: 'invoices#my_action'添加这条路线。

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

此操作返回 vat_number 和 iban_number,它们应根据所选值显示在视图中。

【讨论】:

  • @luissimo,请检查我的回答。
  • 感谢您的帮助,但请注意,选择下拉菜单位于我的发票新视图中,所选数据也应在编辑、索引和显示操作中可见。我用发票控制器编辑了我的 OP。
  • “所选数据应该在编辑、索引和显示操作中可见”,是什么意思?请详细说明。
  • 嗯好吧,但我从 '@customer = current_user.customers.find(params[:id])' 收到此错误 'Couldn't find Customer without an ID' ,与您的确切代码相同的错误.
  • 我已经给(params[:customer])
【解决方案2】:

在customers 模型上创建一个方法,以一种友好的、人类可读的方式将这些信息连接在一起:

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

然后在您的选择语句中使用该方法代替company_name。

【讨论】:

  • 如何在视图中调用“#{company_name} (#{vat_number}) - IBAN #{iban_number}”?分别地。例如,公司 _name 必须位于页面顶部的某个位置,而另外 2 个位于页面底部。
  • 然后分别调用它们。您没有破坏或更改这些属性,您只是制作了一种“方便”的方法来显示它们。您可以在不更改基础数据的情况下创建任意数量的这些方法。
  • 好的,我刚刚看到你的编辑。您希望选择框只有公司名称,但在选择客户后,您希望在页面的其他位置显示其他信息。这是一个更复杂的问题——你必须为此使用 javascript。对不起。
  • 是的,这正是我想要的。我知道 Javascript 是必要的,但我需要先以某种方式拆分属性才能有效地使用 Javascript(jquery)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 2012-06-13
  • 1970-01-01
相关资源
最近更新 更多