Hi All,
I have a form that is creating a ticket model. I am trying to allow
the user to assign employee’s to this newly created ticket model. I
followed the complex form’s railscast, which helped a lot it but it
appears the screencast is for creating two new models, instead of just
creating one model and linking an existing model to it. My problem is
I am just trying to assign employee’s to the ticket model and not
create two new models.
This is the I have so far, which is giving me no errors, it is just
not putting any new data into my employees_tickets table.
TicketsController
class TicketsController < ApplicationController
layout ‘global’
def new
@employees = Employee.find(:all)
@ticket = Ticket.new
@ticket.employees.build
end
def create
@ticket = Ticket.new(params[:ticket])
if @ticket.save
flash[:notice] = “Successfully created ticket.”
redirect_to :action => “list”, :controller => “workorders”
else
render :action => ‘new’
end
end
end
Ticket Model
class Ticket < ActiveRecord::Base
has_and_belongs_to_many :employees
end
Employee Model
class Employee < ActiveRecord::Base
has_and_belongs_to_many :tickets
end
Create Ticket new.rhtml
Create Dispatch Ticket
<% form_for :ticket, :url => { :action => :create } do |form| %>
Address <%= text_field :ticket, :address %>
City <%= text_field :ticket, :city %>
Employees
<% for employee in @ticket.employees %>
<% fields_for “ticket.employee”, employee do |employee_form| %>
<%= employee.name %> | <%= employee_form.check_box :id %> |
<% end %>
<% end %>
<%= submit_tag "Create", :class => "submit" %>
<% end if @ticket.new_record? %>