So I’ve been holding off putting a question on here because I don’t want to bother the community with stupid questions, but I’m going to ask for help now anyway.
I’m quite new to Ruby on Rails, and as you’ve probably read from the title, I’m having trouble with my subform. More specifically, with assigning the parent object to a client object. I’m building a system for my work in where employees can register repairs (mobile phones) and keep track of them. I’m building the client object with @repair = Repair.new, which works fine, but when I try to set the Client with @repair = Client.new, the :client_id on the repair stays null.
Here’s my repair.rb: (some fields are in Dutch, please ignore that)
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
client.rb:
class Client < ActiveRecord::Base
attr_accessible :email, :firstname, :lastname, :number, :phone, :postalcode
has_many :repairs
end
repairs_controller.rb: (I’ve left the irrelevant methods out, I was getting tired of the 4 spaces :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
And this is the JSON I get from /repair/new.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}
By the way, the branch assignment works flawlessly… (It’s null now because I’m not on the IP I specified in the new method)
Please help me out… 🙁
Robin
Solved it!!
The code above all works flawlessly, the problem was a
<%instead of<%=in my view, which made my subform not show up. Duhh.