Rails 4包含simple_form_for,has_many和嵌套属性

时间:2015-11-03 08:16:25

标签: ruby-on-rails ruby-on-rails-4 activerecord nested-attributes has-many-through

我需要几乎与此相同的功能:

https://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through

我一直在四处走动(并且周围)试图让它正常运行,但一直遇到障碍。我仍然对Ruby和Rails有点新意,并且需要向前发展提供帮助。这是我目前存在的实现:

/models/transfer.rb

class Transfer < ActiveRecord::Base

  validates :name, presence:   true,
                   uniqueness: { case_sensitive: false }

  has_many  :transfer_accounts, inverse_of: :transfer
  has_many  :accounts,          through:    :transfer_accounts

  accepts_nested_attributes_for :transfer_accounts

end


/models/transfer_account.rb

class TransferAccount < ActiveRecord::Base

  validates :account_transfer_role, presence: true

  belongs_to :account,  inverse_of: :transfer_accounts
  belongs_to :transfer, inverse_of: :transfer_accounts

  validates :account,  presence: true
  validates :transfer, presence: true

  accepts_nested_attributes_for :account

end


/models/account.rb

class Account < ActiveRecord::Base

  validates :name,           presence:   true,
                             uniqueness: { case_sensitive: false }
  validates :user_name,      presence:   true
  validates :password,       presence:   true
  validates :account_number, presence:   true,
                             uniqueness: { case_sensitive: false }
  validates :routing_number, presence:   true

  has_many :transfer_accounts, inverse_of: :account
  has_many :transfers,         through:    :transfer_accounts

  belongs_to :bank, inverse_of: :accounts

end


/models/bank.rb

class Bank < ActiveRecord::Base

  validates :name,        presence:   true,
                          uniqueness: { case_sensitive: false }
  validates :connect_uri, presence:   true

  has_many :accounts

end


/controllers/transfers_controller.rb

class TransfersController < ApplicationController

  def new
    @transfer = Transfer.new
    @transfer.transfer_accounts.build(account_transfer_role: 'source').build_account
    @transfer.transfer_accounts.build(account_transfer_role: 'destination').build_account
    @valid_banks = Bank.all.collect {|c| [c.name, c.id]}  # available banks seeded in database
  end

  def index
    @transfers = Transfer.all
  end

  def show
    @transfer = resource
  end

  def create
    @transfer = Transfer.new(transfer_params)
    if @transfer.save
      redirect_to transfers_path, notice: "Transfer Created"
    else
      redirect_to transfers_path, alert:  "Transfer Not Created"
    end
  end

  def edit
    resource
  end

  def update
    if resource.update_attributes(transfer_params)
      redirect_to transfers_path(resource),     notice: "Transfer Updated"
    else
      redirect_to edit_transfer_path(resource), alert:  "Transfer Not Updated"
    end
  end

  def destroy
    resource.destroy
  end


  private

  def resource
    @transfer ||= transfer.find(params[:id])
  end

  def transfer_params
    params.require(:transfer).
      permit(:name, :description,
             transfer_accounts_attributes:
               [:account_transfer_role,
                account_attributes:
                  [:name, :description, :user_name, :password,
                   :routing_number, :account_number
                  ]
               ])
  end

end


/controllers/banks_controller.rb

class BanksController < ApplicationController

  def index
    @bank = Bank.new
    @banks = Bank.by_last_updated_at
  end

  def show
    @bank = resource
  end

  def create
    @bank = Bank.new(bank_params)
    if @bank.save
      redirect_to banks_path, notice: "Bank Created"
    else
      redirect_to banks_path, alert: "Bank Not Created"
    end
  end

  def edit
    resource
  end

  def update
    if resource.update_attributes(bank_params)
      redirect_to banks_path(resource), notice: "Bank Updated"
    else
      redirect_to edit_bank_path(resource), alert: "Bank Not Updated"
    end
  end

  def destroy
    resource.destroy
  end


  private

  def resource
    @bank ||= Bank.find(params[:id])
  end

  def bank_params
    params.require(:bank).
      permit(:name, :description, :connection_uri)
  end

end


/views/transfers/_form.html.haml

= simple_form_for :transfer do |t|
  .form-inputs

    = t.input :name, label: "Transfer Name"
    = t.input :description, required: false, label: "Transfer Description"

    = t.simple_fields_for :transfer_accounts do |ta|

      - role = ta.object.account_transfer_role.titleize

      = ta.input :account_transfer_role, as: :hidden

      = ta.simple_fields_for :account do |a|

        = a.input :bank_id, collection:    @valid_banks,
                            include_blank: 'Select bank...',
                            id:            'bank',
                            class:         'bank_selector',
                            label:         '#{role} Bank',
                            error:         '#{role} bank selection is required.'

        = a.input :name, label: "#{role} Account Name"
        = a.input :description, required: false, label: "#{role} Account Description"
        = a.input :user_name, label: "#{role} Account User Name"
        = a.input :password, label: "#{role} Account Password"
        = a.input :account_number, label: "#{role} Account Number"
        = a.input :routing_number, label: "#{role} Account Routing Number"

  = t.submit


/db/migrate/20151102001000_create_transfers.rb

class CreateTransfers < ActiveRecord::Migration
  def change
    create_table :transfers do |t|

      t.string :name, null: false, default: ''
      t.text   :description

      t.timestamps

    end
  end
end


/db/migrate/20151102002000_create_transfer_accounts.rb

class CreateTransferAccounts < ActiveRecord::Migration
  def change
    create_table :transfer_accounts do |t|

      t.string :account_transfer_role, null: false, default: ''

      t.references :transfer, index: true
      t.references :account,  index: true

      t.timestamps null: false

    end
  end
end


/db/migrate/20151102003000_create_accounts.rb

class CreateAccounts < ActiveRecord::Migration
  def change
    create_table :accounts do |t|

      t.string  :name,           null: false, default: ''
      t.string  :description
      t.string  :user_name,      null: false, default: ''
      t.string  :password,       null: false, default: ''
      t.string  :account_number, null: false, default: ''
      t.string  :routing_number, null: false, default: ''

      t.references :bank, index: true

      t.timestamps

    end
  end
end


/db/migrate/20151102004000_create_banks.rb

class CreateBanks < ActiveRecord::Migration
  def change
    create_table :banks do |t|

      t.string :name,           null: false, default: ''
      t.string :description
      t.string :connection_uri, null: false, default: ''

      t.timestamps

    end
  end
end


/db/migrate/20151102005000_add_foreign_keys_to_transfer_accounts.rb

class AddForeignKeysToTransferAccounts < ActiveRecord::Migration
  def change

    add_foreign_key :transfer_accounts, :accounts
    add_foreign_key :transfer_accounts, :transfers

  end
end


/db/migrate/20151102006000_add_foreign_keys_to_accounts.rb

class AddForeignKeysToAccounts < ActiveRecord::Migration
  def change

    add_foreign_key :accounts, :banks

  end
end


/db/seeds.rb

Bank.create(name:           'Acme Savings and Loan',
            description:    'The number one bank in the northeast',
            connection_uri: 'https://www.acmesavings.com')
Bank.create(name:           'First Bank of Anytown',
            description:    'The first and only bank in Anytown',
            connection_uri: 'https://www.firstbankanytown.com')
Bank.create(name:           'Generibank',
            description:    'The most generic bank in the country',
            connection_uri: 'https://www.generibank.com')


/config/routes.rb

Rails.application.routes.draw do

  resources  :transfers
  resources  :accounts
  resources  :banks
  root to:   'dashboard#index'

end

所以,目前我的问题是:

  1. 表单视图中的此行- role = ta.object.account_transfer_role.titleize
    给我一个“未定义的方法`account_transfer_role'为nil:NilClass ”错误,所以我在那里做错了什么?

  2. 为什么(或者为什么会)accepts_nested_attributes_for模型中的 transfer_account 行有效?我认为accepts_nested_attributes_for在关联的belongs_to方面不起作用,因为它不是父级(或者是那种效果)。

    < / LI>
  3. 如果我注释掉与问题#1相关的代码以避免该错误,则表单呈现,但我只获得account嵌套属性的一组输入框。每个transfertransfer_accounts的“”操作中构建并与之关联accounts和两个transfers_controllertransfer_account 1}} :account_transfer_role'来源'和'目的地'的值,我不应该得到两组account嵌套属性输入框?

  4. 我的嵌套属性的单数/复数是否正确?基本上,我从他们的协会开始就保持一致。例如,transfer has_many :transfer_accounts,所以transfer_accounts在以下所有方面都是复数:

    /models/transfer.rb
    
      accepts_nested_attributes_for :transfer_accounts
    
    
    /controllers/transfers_controller.rb
    
      @transfer.transfer_accounts.build(account_transfer_role: 'source').build_account  
      @transfer.transfer_accounts.build(account_transfer_role: 'destination').build_account  
      .
      .
      .
      def transfer_params
        params.require(:transfer).
          permit(:name, :description,
                 transfer_accounts_attributes:
                   [:account_transfer_role,
                    account_attributes:
                      [:name, :description, :user_name, :password,
                       :routing_number, :account_number
                      ]
                   ])
      end
    
    
    /views/transfers/_form.html.haml
    
      = m.simple_fields_for :transfer_accounts do |ma|  
    

    account也是如此,除了单数。

  5. 如果我执行第3步,请使用提供的一组account属性填写表单,然后提交,我得到'没有路由匹配[POST]“/ transfers / new '错误。所以,我的路由显然有问题。我不确定transfersaccounts应该如何出现在routes.rb文件中。作为

    resources  :transfers
    resources  :accounts
    

    resources  :transfers do  
      resources  :accounts  
    end  
    

    或者还有其他一些方式。另外,我也不知道路由文件中是否还需要transfer_accounts

  6. 如果您已经接受了这一行,请感谢您的耐心等待。:&gt;)我们将非常感谢您提供的任何帮助。

    干杯, 蒂姆

1 个答案:

答案 0 :(得分:1)

解决方案非常简单,解决了我在问题1,3,4和5中提出的每个问题。

在表格中,我改变了这一行:

= simple_form_for :transfer do |t|

到此:

= simple_form_for @transfer do |t|

对于问题1,account_transfer_role不再是零 对于问题3,它开始渲染transfer_accounts的输入框 对于问题4,强参数中的单数和复数证明是正确的,如图所示 对于问题5,这个版本的路线被证明是正确的:

resources  :transfers
resources  :accounts

虽然我把它简化为:

resources  :transfers, :accounts

关于问题2,我提出的最佳答案是我的理解似乎不正确,accepts_nested_attributes_for:确实在协会的belongs_to方面有效,因为它正在为我

我希望这对于那些为has_many, through关联嵌套属性的表单而苦恼的人有用。

干杯!