HABTM check_box + create

Hi,

I have the following models:

Class Question
has_and_belongs_to_many :domains
end

Class Domain
has_and_belongs_to_many :questions
end

Class Category
belongs_to :question
end

I have a ‘domains’ table with a list of 5 domains, and i also have a
‘domains_questions’ join table.

I want to be able to create a new Question and assign the Question to
multiple Domains using a check box.

This is what i have in the questions/new.rhtml file:

Domain:
<% @domain.each do |domain| %>

<%= check_box_tag "question[domain_ids][]", domain.id, @question.domains.include?(domain) %> <%= domain.name %>
<% end %> ...

This is what i have in the questions_controller.rhtml file:

def new
@question = Question.new
@category = Category.find(:all)
@domain = Domain.find(:all)
end

def create
params[:question][:domain_ids] ||= []
@question = Question.new(params[:question][:category_id]
[:domain_ids])
@question.user = User.find(current_user)
respond_to do |format|
if @question.save_with_captcha
flash[:notice] = ‘Question was successfully created.’
format.html { redirect_to question_path(@question) }
else
@category = Category.find(:all)
format.html { render :action => “new” }
end
end
end

I get this error…

NoMethodError in Questions#create
Showing questions/new.rhtml where line #14 raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #14):

11:
12:


13: Domain:

14: <% @domain.each do |domain| %>
15:


16: <%= check_box_tag “question[domain_ids][]”, domain.id,
@question.domains.include?(domain) %>
17: <%= domain.name %>

Does anyone know what i should be entering into the create action and
how i should modify the view???

Many thanks,

PJ.

thanks for the information i want to full structure of the project