所以我一直推迟在这里提出问题,因为我不想用愚蠢的问题打扰社区,但无论如何我现在要寻求帮助。
我对 Ruby on Rails 很陌生,正如您可能从标题中看到的那样,我的子表单遇到了问题。更具体地说,将父对象分配给客户端对象。我正在为我的工作建立一个系统,员工可以在其中注册维修(手机)并跟踪它们。我正在使用 构建客户端对象@repair = Repair.new
,效果很好,但是当我尝试设置Client
with@repair = Client.new
时,:client_id
修复中的 仍然存在null
。
这是我的repair.rb:(有些字段是荷兰语,请忽略)
class Repair < ActiveRecord::Base
attr_accessible :imei, :klantnaam, :telefoon, :intake, :branch_id, :id, :client_id
attr_accessible :merk, :type, :batterij, :lader, :headset, :batterijklep, :carkit, :schade_toestel, :schade_scherm, :bon, :datum_bon, :klacht, :prijsindicatie
belongs_to :branch
belongs_to :client
accepts_nested_attributes_for :client
end
客户端.rb:
class Client < ActiveRecord::Base
attr_accessible :email, :firstname, :lastname, :number, :phone, :postalcode
has_many :repairs
end
repairs_controller.rb:(我已经把不相关的方法排除在外,我已经厌倦了 4 个空格:P)
class RepairsController < ApplicationController
# GET /repairs/new
# GET /repairs/new.json
def new
@repair = Repair.new
@repair.client = Client.new
if request.remote_ip == "xx.xx.xx.xx"
@repair.branch = Branch.where(:name => "Xxxxxxx").first
end
@repair.intake = Time.now
respond_to do |format|
format.html # new.html.erb
format.json { render json: @repair }
end
end
# POST /repairs
# POST /repairs.json
def create
@repair = Repair.new(params[:repair])
respond_to do |format|
if @repair.save
format.html { redirect_to @repair, notice: 'Repair was successfully created.' }
format.json { render json: @repair, status: :created, location: @repair }
else
format.html { render action: "new" }
format.json { render json: @repair.errors, status: :unprocessable_entity }
end
end
end
end
这是我从 /repair/new.json 得到的 JSON:
{"batterij":null,"batterijklep":null,"bon":null,"branch_id":null,"carkit":null,"client_id":null,"created_at":null,"datum_bon":null,"headset":null,"id":null,"imei":null,"intake":"2013-02-01T23:29:10Z","klacht":null,"klantnaam":null,"lader":null,"merk":null,"pickup":null,"prijsindicatie":null,"schade_scherm":null,"schade_toestel":null,"telefoon":null,"updated_at":null}
顺便说一句,分支分配完美无缺......(现在它为空,因为我不在我在new
方法中指定的IP上)
请帮帮我... :-(
罗宾