Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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/23.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 如何设置/限制rails表单中的输入以匹配其他模型表?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何设置/限制rails表单中的输入以匹配其他模型表?

Ruby on rails 如何设置/限制rails表单中的输入以匹配其他模型表?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我对编程非常陌生,一直无法找到任何资源来帮助我。我创建了两个scaffold账户和现金簿。我希望用户能够添加一个账户(参数为account_name),然后将其设置为现金簿账户输入的参数。理想情况下,在添加现金簿交易时,我希望用户看到一个下拉列表,其中包含已创建账户的所有账户名称 现金簿模型 class Cashbook < ActiveRecord::Base belongs_to :account, foreign_key: :account_name end class A

我对编程非常陌生,一直无法找到任何资源来帮助我。我创建了两个scaffold账户现金簿。我希望用户能够添加一个账户(参数为account_name),然后将其设置为现金簿账户输入的参数。理想情况下,在添加现金簿交易时,我希望用户看到一个下拉列表,其中包含已创建账户的所有账户名称

现金簿模型

class Cashbook < ActiveRecord::Base
    belongs_to :account, foreign_key: :account_name
end
class Account < ActiveRecord::Base
    has_many :cashbook, foreign_key: :account_name
end
现金簿数据库

class CreateAccounts < ActiveRecord::Migration
  def change
    create_table :accounts do |t|
      t.string :account_name
      t.boolean :operating_expense
      t.boolean :cost_of_sales
      t.boolean :sales
      t.boolean :other_income
      t.boolean :non_current_liability
      t.boolean :non_current_asset
      t.boolean :current_asset
      t.boolean :current_liability
      t.boolean :equity
      t.integer :account_number

      t.timestamps null: false
    end
  end
end
现金簿数据库

class CreateCashbooks < ActiveRecord::Migration
  def change
    create_table :cashbooks do |t|
      t.date :date
      t.string :description
      t.boolean :account
      t.string :kind
      t.integer :amount

      t.timestamps null: false
    end
  end
end
class CreateCashbooks
您可以使用表单帮助器

在你的情况下,它可能看起来像

<%= collection_select(:cashbook, :account_name, Account.all, :account_name, :account_name) %>

为什么表是通过帐户名而不是ID连接的?
class CreateCashbooks < ActiveRecord::Migration
  def change
    create_table :cashbooks do |t|
      t.date :date
      t.string :description
      t.boolean :account
      t.string :kind
      t.integer :amount

      t.timestamps null: false
    end
  end
end
<%= collection_select(:cashbook, :account_name, Account.all, :account_name, :account_name) %>
<%= collection_select(:cashbook, :account_id, Account.all, :id, :account_name) %>