Hello,
I am very new to ruby and need to create my first HABTM with multiple
select list.
I am having problems saving data in the middle(linking)
table.
1. titles can have multiple categories
2. categories can have multiple titles
Categories, titles and categories_titles tables are created.
----- title_controller.rb--
def new
@title = Title.new
@categories = Category.find(:all, :order
=>"name")
@category_ids = []
@title.categories.each { |x| @category_ids <<
x.id }
end
def create
@title = Title.new(params[:title])
@title.categories <<
Category.find(@params[:category_ids])
if @title.save
flash[:notice] = ''Title was successfully
created.''
redirect_to :action => ''list''
else
render :action => ''new''
end
end
----title.rb---
class Title < ActiveRecord::Base
has_and_belongs_to_many :categories
end
-------------category.rb--
class Category < ActiveRecord::Base
validates_presence_of :name
has_and_belongs_to_many :titles
end
---
----view
Category Mappings:
<% options = options_from_collection_for_select (@categories,
''name'',''id'', @category_ids) %>
<%= select_tag(''categories_ids[]'', options,
{:multiple=>true,
:size=>7}) %>
----
I am getting error:
Couldn''t find Category without an ID
Please help.
Thanks.
Inessa.
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Hi, Inessa Weiner wrote:> @title.categories << Category.find(@params[:category_ids]). . .> <p>Category Mappings:<br> > > <% options = options_from_collection_for_select (@categories, > ''name'',''id'', @category_ids) %> > <%= select_tag(''categories_ids[]'', options, {:multiple=>true, :size=>7}) %> > > ---- > I am getting error: > Couldn''t find Category without an IDI think it should be: @title.categories = Category.find(params[:categories_ids]) if params[:categories_ids] Regards, Ali
Nothing is getting save in the middle table if I am using
@title.categories Category.find(params[:categories_ids]) if
params[:categories_ids]
However trace shows that
"..categories_ids"=>["2"].
if I omit
if params[:categories_ids]
The same error comes up:
Could''t find Category without an ID
Please help.
At 03:20 PM 6/30/2006 -0400, you wrote:
Hi,
Inessa Weiner wrote:
@title.categories << Category.find(@params[:category_ids])
.
.
.
Category
Mappings:
<% options = options_from_collection_for_select (@categories,
''name'',''id'', @category_ids) %>
<%= select_tag(''categories_ids[]'', options,
{:multiple=>true,
:size=>7}) %>
----
I am getting error:
Couldn''t find Category without an ID
I think it should be:
@title.categories = Category.find(params[:categories_ids]) if
params[:categories_ids]
Regards,
Ali
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails