所以,我比上次更进一步。迁移到数据库,但我仍然遇到问题。
我正在通过设计构建一个拥有一个“主用户”的应用程序。此主用户创建我们正在跟踪的员工和项目的记录。然后,我希望能够创建“员工”和“项目”的“交易记录”。因此,当我查看交易时,我可以看到它是谁,它是什么项目,我可以查看员工的视图并查看所有关联交易,以及查看项目的所有关联交易看法。
我已经建立了如下所示的数据库,但出现如下错误:
Transaction#description delegated to item.description, but item is nil: #<Transaction id: 4, status: true, item_id: 12345, employee_id: 1, created_at: "2013-05-22 23:49:09", updated_at: "2013-05-22 23:49:09">
我只是想显示与每个交易记录相关联的项目和员工的所有信息。我不知道我做错了什么,但我认为委托、has_many 和 belongs_to 应该就足够了。我究竟做错了什么?对此,我真的非常感激。
数据库/schema.rb
ActiveRecord::Schema.define(:version => 20130516162824) do
create_table "employees", :force => true do |t|
t.string "phone"
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "items", :force => true do |t|
t.string "description"
t.string "assettag"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "transactions", :force => true do |t|
t.boolean "status"
t.integer "item_id"
t.integer "employee_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "transactions", ["employee_id"], :name => "index_transactions_on_employee_id"
add_index "transactions", ["item_id"], :name => "index_transactions_on_item_id"
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
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", :null => false
t.datetime "updated_at", :null => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
应用程序/模型/transaction.rb
class Transaction < ActiveRecord::Base
attr_accessible :employee_id, :item_id, :status
belongs_to :employee
belongs_to :item
delegate :phone, :name, to: :employee
delegate :description, :assettag, to: :item
end
应用程序/模型/item.rb
class Item < ActiveRecord::Base
attr_accessible :assettag, :description
has_many :transactions
end
应用程序/模型/employee.rb
class Employee < ActiveRecord::Base
attr_accessible :name, :phone
has_many :transactions
end
app/views/transaction/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Status:</b>
<%= @transaction.status %>
</p>
<p>
<b>Item </b>
<%= @transaction.item_id %>
</p>
<p>
<b>Item </b>
<%= @transaction.description %>
</p>
<p>
<b>Employee</b>
<%= @transaction.name %>
</p>
<%= link_to 'Edit', edit_transaction_path(@transaction) %> |
<%= link_to 'Back', transactions_path %>