Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从不同的模型中选择列并在视图中显示属性_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 从不同的模型中选择列并在视图中显示属性

Ruby on rails 从不同的模型中选择列并在视图中显示属性,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我有一个脚手架“Customer.rb”模型和一个“Invoice.rb”模型。我希望人们从客户模型中选择一个现有客户,并在发票视图中显示该选项的一些属性 这是发票控制器中我的Invoicenew@customer=customer.all中的代码 <%= @customer.select(:company_name) do |f| %> <h4>Customer:</h4>

我有一个脚手架“Customer.rb”模型和一个“Invoice.rb”模型。我希望人们从客户模型中选择一个现有客户,并在发票视图中显示该选项的一些属性

这是发票控制器中我的Invoicenew@customer=customer.all中的代码

                <%= @customer.select(:company_name) do |f| %>
                    <h4>Customer:</h4>
                    <div class="well">
                      <address>
                        <strong class="text-dark"><%= f.company_name %></strong><br/>
                        <%= f.first_name + f.last_name if f.first_name.present? && f.last_name.present?%><br/>
                        <%= f.address_line_1 %><br/>
                        <%= f.address_line_2 if present? %>
                      </address>
                    </div>
                <% end %>
              </div>
我尝试了RubyonRails指南中的所有内容,但似乎都不懂。任何帮助都将不胜感激

更新

发票及表格


你想要的比你想象的要复杂得多。任何时候你想要动态的东西,你都必须依靠脚本。视图基本上是惰性的——它只显示由控制器提供给它的信息,因此您需要JavaScript和AJAX的帮助

首先,在invoicenew中将字段名复数,以避免查看代码的人可能会感到困惑

@customer = Customer.all
应该是:

@customers = Customer.all
这样,很明显,您指的是一个集合,而不是一条记录

然后将视图中的选择替换为以下内容:

<%= f.select :customer, options_for_select(@customers.map { | cust | [cust.name, cust.id, 'data-some-field' => cust.some_field] }), { id: "my_select" } %>
然后,您必须使用以下JavaScript修改invoice.js资产:

$("#my_select").change(function()
{
    var selection = $(this).find(":selected");
    getElementById("my_field").value = selection.data("data-some-field");
});

你想要的比你想象的要复杂得多。任何时候你想要动态的东西,你都必须依靠脚本。视图基本上是惰性的——它只显示由控制器提供给它的信息,因此您需要JavaScript和AJAX的帮助

首先,在invoicenew中将字段名复数,以避免查看代码的人可能会感到困惑

@customer = Customer.all
应该是:

@customers = Customer.all
这样,很明显,您指的是一个集合,而不是一条记录

然后将视图中的选择替换为以下内容:

<%= f.select :customer, options_for_select(@customers.map { | cust | [cust.name, cust.id, 'data-some-field' => cust.some_field] }), { id: "my_select" } %>
然后,您必须使用以下JavaScript修改invoice.js资产:

$("#my_select").change(function()
{
    var selection = $(this).find(":selected");
    getElementById("my_field").value = selection.data("data-some-field");
});

我认为您应该将两者都与联接表关联。。联接表将保留客户和发票id的记录

class Customer < ApplicationRecord
has_many :join_table 
has_many :invoices, :through => :join_table 
end 

class Invoice < ApplicationRecord
has_many :join_table
has_many :customers, :through => :join_table
end 

class JoinTable < ApplicationRecord
belongs_to :invoice
belongs_to :customer
end  
然后在发票和客户创建操作中

def create
@invoice = Invoices.new(invoice_params) 
if @invoice.save
  @invoice.customers << Customer.all
    redirect_to @invoice
else 
    render new 
end         
结束

这将创建连接表对象,该对象将链接每个客户及其ID的发票

然后调整join_table.rb模型

class JoinTable < ActiveRecord::Base
after_create :add_customer_values

def add_customer_values 
customer = Customer.find self.customer_id
self.value1 = customer.value1
self.value2 = customer.value2
self.save 
end 

然后,您应该在invoice show页面中有一个所有客户的列表,并将join_table记录显示为一个伪值,但值相同,我认为您应该将两者与一个join table关联。。联接表将保留客户和发票id的记录

class Customer < ApplicationRecord
has_many :join_table 
has_many :invoices, :through => :join_table 
end 

class Invoice < ApplicationRecord
has_many :join_table
has_many :customers, :through => :join_table
end 

class JoinTable < ApplicationRecord
belongs_to :invoice
belongs_to :customer
end  
然后在发票和客户创建操作中

def create
@invoice = Invoices.new(invoice_params) 
if @invoice.save
  @invoice.customers << Customer.all
    redirect_to @invoice
else 
    render new 
end         
结束

这将创建连接表对象,该对象将链接每个客户及其ID的发票

然后调整join_table.rb模型

class JoinTable < ActiveRecord::Base
after_create :add_customer_values

def add_customer_values 
customer = Customer.find self.customer_id
self.value1 = customer.value1
self.value2 = customer.value2
self.save 
end 

然后,您应该在“发票显示”页面中列出所有客户的列表,并将join_表记录显示为虚拟但值相同的记录

请发布发票控制器New的全部内容。看起来你只想要一条记录,却要删除一个集合,但我想查看代码以确定。已将完整的发票控制器添加到OP中,感谢您的回复。抱歉,我想我误解了您的意图。在您的发票的new.html.erb中,您可以选择已发布的客户名称。用户选择一个客户,您希望在同一页面上动态显示该客户的信息。当用户选择不同的客户时,您只想显示新客户选择的信息,对吗?是的,这是正确的。请发布新发票的全部内容。看起来你只想要一条记录,却要删除一个集合,但我想查看代码以确定。已将完整的发票控制器添加到OP中,感谢您的回复。抱歉,我想我误解了您的意图。在您的发票的new.html.erb中,您可以选择已发布的客户名称。用户选择一个客户,您希望在同一页面上动态显示该客户的信息。当用户选择另一个客户时,您只想显示新客户选择的信息,对吗?是的,没错。感谢您的回复,但使用该代码我会得到此错误;模板包含跟踪的未定义方法“merge”:app/views/invoices/new.html.erb我使用了此代码;customer.company\u name}]},id:my\u select%>我可能自己搞砸了,我在脚手架上添加了整个发票表单的一部分,这样你可以看得更清楚。不,你没有搞砸。我保存了一个代码片段文件,并试图根据您的需要对其进行修改。请参阅新选择。经过测试,不会抛出错误;未定义的方法'customer\u id'for-未定义的方法'company\u name'感谢您的回复,但是使用该代码我得到了这个错误;模板包含跟踪的未定义方法“merge”:app/views/invoices/new.html.erb我使用了此代码;customer.company_name}]},
id:my_select%>可能是我自己搞砸了,我在脚手架上添加了整个发票表单部分,这样你可以看得更清楚。不,你没有搞砸。我保存了一个代码片段文件,并试图根据您的需要对其进行修改。请参阅新选择。经过测试,不会抛出错误;未定义的方法'customer_id'for-未定义的方法'company_name'感谢您的回复,但这不会让我处于与我现在所处的情况完全相同的情况下,但是使用不同的模型,不知道如何从列表中进行选择并基于该选择显示属性。。对不起,到目前为止,我还没有那么丰富的网络应用经验。不,它不会。。。联接表将包含您要选择的特定客户的记录,因此,当您选择联接表时,显示页面将包含您选择的客户的详细信息。这里唯一的问题是,luissimo的问题基本上是一个UI问题,因为ini如何显示…,而不是一个数据库设计问题。客户和发票之间的关系是一对多的关系。联接表使其成为多对多,虽然您仍然可以使用像这样的映射作为一对多,但这太过分了。感谢您的回复,但这不会让我处于与我现在所处的完全相同的情况,但是使用不同的模型,不知道如何从列表中选择并基于该选择显示属性。。对不起,到目前为止,我还没有那么丰富的网络应用经验。不,它不会。。。联接表将包含您要选择的特定客户的记录,因此,当您选择联接表时,显示页面将包含您选择的客户的详细信息。这里唯一的问题是,luissimo的问题基本上是一个UI问题,因为ini如何显示…,而不是一个数据库设计问题。客户和发票之间的关系是一对多的关系。联接表使其成为多对多,虽然您仍然可以将这样的映射作为一对多使用,但这太过分了。