# HABTM Problem

**URL:** <https://www.ruby-forum.com/t/habtm-problem/123790>\
**Category:** Rails\
**Created:** [December 20, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/habtm-problem/123790 "2007-12-20T17:40:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [December 20, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/habtm-problem/123790/1 "2007-12-20T17:40:49Z")

</div>

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| %\>

  
\<% for employee in @employees %\>  
  
  
  
\<% end %\>  

| \<%= employee.name %\> | \<%= employee\_form.check\_box  
:id  
%\> |

  
\<% end %\>  
\<% end %\>  
\<%= submit\_tag "Create", :class =\> "submit" %\>

\<% end if @ticket.new\_record? %\>

---

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [December 20, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/habtm-problem/123790/2 "2007-12-20T18:52:54Z")

</div>

I tried simplifying things a bit but no luck either.

View:

\<% for employee in @employees %\> \<% end %\>

| \<%= employee.name %\> | \<%= check\_box\_tag ("ticket[employee\_ids][]", employee.id) %\> |

Controller:  
class TicketsController \< ApplicationController  
layout ‘global’  
def new  
@employees = Employee.find(:all)  
@ticket = Ticket.new  
end

def create  
@ticket = Ticket.new(params[:ticket])  
@ticket.employees = Employee.find(@params[:employee\_ids])  
if @ticket.save  
flash[:notice] = “Successfully created ticket.”  
redirect\_to :action =\> “list”, :controller =\> “workorders”  
else  
render :action =\> ‘new’  
end  
end

Error:  
NoMethodError in TicketsController#create

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.[]

---

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [December 20, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/habtm-problem/123790/3 "2007-12-20T19:53:48Z")

</div>

Recently I was implementing PDF generation for a project utilizing the  
fantastic library Prince XML ([http://www.princexml.com](http://www.princexml.com)). I came across  
a blog article with a basic library and helper set for Prince  
([http://sublog.subimage.com/articles/2007/05/29/html-css-to-pdf-using-ruby-on-rails](http://sublog.subimage.com/articles/2007/05/29/html-css-to-pdf-using-ruby-on-rails)  
), which provided a great basis. I wanted to make something a little  
more generalized and in-keeping the Rails Way, so I have created  
‘Princely’, a simple wrapper utilizing much of the code from the  
SubImage library but giving it better helpers and pluginizing its  
inclusion.

== Installation ==

The first step is to download Prince ([Prince - Download Prince 15.1](http://princexml.com/download/))  
and install it on your platform of choice (only Linux and Mac OS X  
supported by the plugin at this time). Next, simply install the plugin:

script/install plugin [http://svn.intridea.com/svn/public/princely](http://svn.intridea.com/svn/public/princely)

You are now ready to get started using Princely to generate PDF. Note  
that Princely is only compatible with Rails \>= 2.0

== Usage ==

Princely uses the MimeTypes and respond\_to blocks from Rails 2.0 to  
add PDF as a render option and a format. Because of this, it’s  
incredibly easy to implement a PDF! Simply make your XHTML or XML  
template and use pdf as the format (e.g. show.pdf.erb), then add code  
similar to this in your controller:

class PagesController \< ApplicationController  
def show  
respond\_to do |format|  
format.html  
format.pdf {  
render :pdf =\> @page.pdf\_name,  
:template =\> “show.pdf.erb”, # not required, shown for  
example  
:layout =\> false # not required  
}  
end  
end  
end

And that’s all there is to it! If you add a .pdf to your properly  
routed path, you should be presented with a PDF version of the page  
generated by Prince. The README  
([http://svn.intridea.com/svn/public/princely/README](http://svn.intridea.com/svn/public/princely/README)  
) has more detailed usage information.

There is a Trac ([http://trac.intridea.com/trac/public/](http://trac.intridea.com/trac/public/)) available for  
any bugs or patches you might come across. Additionally you can  
comment on this at the original blog post  
([http://www.intridea.com/2007/12/20/announcing-princely-rails-prince-xml-pdf-wrapper](http://www.intridea.com/2007/12/20/announcing-princely-rails-prince-xml-pdf-wrapper)  
).
