简体   繁体   English

找不到Rails简单表单关联

[英]Rails Simple Form Association Not Found

I am using Simple Form and the Invoicing gem, but when I try to render the form, I get "RuntimeError in InvoicingLedgerItems#new" with "Association :sender_id not found". 我正在使用简单表单和Invoicing gem,但是当我尝试呈现表单时,出现“ InvoicingLedgerItems#new中的RuntimeError”和“ Association:sender_id not found”。 I want to save the primary key of the Users table (Devise gem) in the sender_id field (because the user is the sender of the invoice). 我想将用户表(Devise gem)的主键保存在sender_id字段中(因为用户是发票的发件人)。 I tried using the foreign_key option in the model, but it didn't seem to have any effect. 我尝试在模型中使用foreign_key选项,但似乎没有任何效果。 Here's my code without using the foreign_key option. 这是我的代码,没有使用foreign_key选项。

Models: 楷模:

class InvoicingLedgerItem < ActiveRecord::Base
  acts_as_ledger_item

  belongs_to :user
  has_many :line_items, class_name: 'InvoicingLineItem', foreign_key: :ledger_item_id
  accepts_nested_attributes_for :line_items
end

class User < ActiveRecord::Base
  has_many :invoicing_ledger_items
end

View: 视图:

<%= simple_form_for @invoicing_ledger_item do |f| %>
  <%= f.association :sender_id %>
  <%= f.button :submit %>
<% end %>

Schema: 架构:

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.datetime "deleted_at"
  end

  create_table "invoicing_ledger_items", force: true do |t|
    t.integer  "sender_id"
    t.integer  "recipient_id"
    t.string   "type"
    t.datetime "issue_date"
    t.string   "currency",     limit: 3,                           null: false
    t.decimal  "total_amount",            precision: 20, scale: 4
    t.decimal  "tax_amount",              precision: 20, scale: 4
    t.string   "status",       limit: 20
    t.string   "identifier",   limit: 50
    t.string   "description"
    t.datetime "period_start"
    t.datetime "period_end"
    t.string   "uuid",         limit: 40
    t.datetime "due_date"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

You Need to Make a relation with user model through foreign key 'sender_id' 您需要通过外键“ sender_id”与用户模型建立联系

Model 模型

class InvoicingLedgerItem < ActiveRecord::Base  
  acts_as_ledger_item  
  belongs_to :user, foreign_key: :sender_id  
  has_many :line_items, class_name: 'InvoicingLineItem', foreign_key: :ledger_item_id  
  accepts_nested_attributes_for :line_items  
end

Need to make association with user as hidden field 需要与用户关联为隐藏字段

View: 视图:

<%= simple_form_for @invoicing_ledger_item do |f| %>  
  <%= f.association :user, :as => :hidden, :input_html => { :value => curren_user.id } %>  
  <%= f.button :submit %>  
<% end %>

You should use user_id instead of sender_id in the schema first.Then use alias_attribute :user_id, :sender_id in InvoicingLedgerItem model, through this helper method you can assign a new name to the field name. 您应该sender_id在架构中使用user_id而不是sender_id alias_attribute :user_id, :sender_idInvoicingLedgerItem模型中使用alias_attribute :user_id, :sender_id ,通过此辅助方法可以为字段名称分配新名称。

For more reference visit http://api.rubyonrails.org/classes/Module.html . 有关更多参考,请访问http://api.rubyonrails.org/classes/Module.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM