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
<h1>Create Dispatch Ticket</h1>
<% form_for :ticket, :url => { :action => :create } do |form| %> <p> <label class="inputlabel">Address</label> <%= text_field :ticket, :address %> </p>
<p> <label class="inputlabel">City</label> <%= text_field :ticket, :city %> </p>
<h2>Employees</h2>
<% for employee in @ticket.employees %> <% fields_for "ticket.employee", employee do |employee_form| %> <table style="width: 200px"> <% for employee in @employees %> <tr> <td><%= employee.name %></td><td><%= employee_form.check_box :id %></td> </tr> <% end %> </table> <% end %> <% end %>
<br> <%= submit_tag "Create", :class => "submit" %>
<% end if @ticket.new_record? %>