我正在尝试使用RoR中的form_for帮助器生成表单,但我遇到的似乎是路由错误.以下是相关文件:
车型/ equipment.rb
class Equipment < ActiveRecord::Base
attr_accessible :name, :tracking_number
validates :tracking_number, :presence => true,
:uniqueness => { :case_sensitive => true }
end
Run Code Online (Sandbox Code Playgroud)
控制器/ equipments_controllers.rb
class EquipmentsController < ApplicationController
def index
@equipments = Equipment.paginate(:page => params[:page])
end
def new
@equipment = Equipment.new
end
end
Run Code Online (Sandbox Code Playgroud)
视图/设备/ new.html.rb
<h1>Add an equipment</h1>
<%= form_for (@equipment) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %> <br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :tracking_number …Run Code Online (Sandbox Code Playgroud) 我有两个型号,项目和产品如下:
irb(main):007:0> Item
=> Item(id: integer, identification_number: string, production_date: date,
created_at: datetime, updated_at: datetime, going_in: boolean)
irb(main):008:0> Product
=> Product(id: integer, sku: string, barcode_identification: string,
created_at: datetime, updated_at: datetime)
Run Code Online (Sandbox Code Playgroud)
您可以将其视为特定产品.
我已经设法通过引用特定产品(Product.find(1).items)的所有项目
class Product < ActiveRecord::Base
has_many :items, :foreign_key => "identification_number",
:primary_key => "barcode_identification"
end
Run Code Online (Sandbox Code Playgroud)
但我似乎无法推荐特定产品的产品.这就是我现在所拥有的:
class Item < ActiveRecord::Base
set_primary_key :identification_number
belongs_to :product, :foreign_key => "barcode_identification"
end
Run Code Online (Sandbox Code Playgroud)
就我的理解而言:数据库,这应该工作.除了它没有.也许我错过了这里的一些东西?我对rails很新(大约一个月或更短的时间.)