I know it has been asked many times, but i could not find any working
examples even after 2 days for googling. So, i thought let me ask here.
How do I save in HABTM
Its a simple application where users should be able to post recipes and
that recipe HABTM categories. Multiple categories are selected through
multi select list box.
1. I have 2 models: Recipe and Category
2. I have one join table: categories_recipes (category_id, recipe_id)
Now this is in my controller:
class RecipesController < ApplicationController
  def new
    @recipe = Recipe.new
    @categories = Category.find(:all)
  end
  def create
    @recipe = Recipe.new(params[:recipe])
    @recipe.category_recipe_ids[]<<(params[:category_recipe_ids])
    @recipe.save
    redirect_to :action=>''show'', :id=>@recipe.id
  end
  def show
    @recipe=Recipe.find(params[:id])
  end
end
My new.html.erb
<% form_for :recipe, @recipe, :url => { :action => "create" }
do |f| %>
  <%= f.text_field :title, :maxlength => 100 %><br>
  <%= f.text_area :description, :rows=>''xx'',
:cols=>''yy'' %><br>
<%= select_tag ''category_recipe_ids[]'',
options_from_collection_for_select(@categories, :id, :title), {
:multiple => true, :size =>5, :id => "categories_recipes_id"
} %></p>
  <%= submit_tag ''save'',
:style=>''font-size:14px;font-weight:bold;width:100px''
%><br>
<% end %>
Any help is much appreciated.
-- 
Posted via http://www.ruby-forum.com/.
I''m new to rails and will be interested how this works out, so I''ll be watching this thread. What errors are you getting? On Wed, Jul 15, 2009 at 6:12 AM, Rails List < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I know it has been asked many times, but i could not find any working > examples even after 2 days for googling. So, i thought let me ask here. > How do I save in HABTM > > Its a simple application where users should be able to post recipes and > that recipe HABTM categories. Multiple categories are selected through > multi select list box. > > 1. I have 2 models: Recipe and Category > 2. I have one join table: categories_recipes (category_id, recipe_id) > > Now this is in my controller: > > class RecipesController < ApplicationController > def new > @recipe = Recipe.new > @categories = Category.find(:all) > end > > def create > @recipe = Recipe.new(params[:recipe]) > @recipe.category_recipe_ids[]<<(params[:category_recipe_ids]) > @recipe.save > redirect_to :action=>''show'', :id=>@recipe.id > end > > def show > @recipe=Recipe.find(params[:id]) > end > end > > My new.html.erb > > <% form_for :recipe, @recipe, :url => { :action => "create" } do |f| %> > <%= f.text_field :title, :maxlength => 100 %><br> > <%= f.text_area :description, :rows=>''xx'', :cols=>''yy'' %><br> > <%= select_tag ''category_recipe_ids[]'', > options_from_collection_for_select(@categories, :id, :title), { > :multiple => true, :size =>5, :id => "categories_recipes_id" } %></p> > <%= submit_tag ''save'', > :style=>''font-size:14px;font-weight:bold;width:100px'' %><br> > <% end %> > > Any help is much appreciated. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Rick R --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Could you also paste your models and migrations? On Wed, Jul 15, 2009 at 3:30 PM, Rick <rickcr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m new to rails and will be interested how this works out, so I''ll be > watching this thread. > > What errors are you getting? > > > On Wed, Jul 15, 2009 at 6:12 AM, Rails List < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> >> I know it has been asked many times, but i could not find any working >> examples even after 2 days for googling. So, i thought let me ask here. >> How do I save in HABTM >> >> Its a simple application where users should be able to post recipes and >> that recipe HABTM categories. Multiple categories are selected through >> multi select list box. >> >> 1. I have 2 models: Recipe and Category >> 2. I have one join table: categories_recipes (category_id, recipe_id) >> >> Now this is in my controller: >> >> class RecipesController < ApplicationController >> def new >> @recipe = Recipe.new >> @categories = Category.find(:all) >> end >> >> def create >> @recipe = Recipe.new(params[:recipe]) >> @recipe.category_recipe_ids[]<<(params[:category_recipe_ids]) >> @recipe.save >> redirect_to :action=>''show'', :id=>@recipe.id >> end >> >> def show >> @recipe=Recipe.find(params[:id]) >> end >> end >> >> My new.html.erb >> >> <% form_for :recipe, @recipe, :url => { :action => "create" } do |f| %> >> <%= f.text_field :title, :maxlength => 100 %><br> >> <%= f.text_area :description, :rows=>''xx'', :cols=>''yy'' %><br> >> <%= select_tag ''category_recipe_ids[]'', >> options_from_collection_for_select(@categories, :id, :title), { >> :multiple => true, :size =>5, :id => "categories_recipes_id" } %></p> >> <%= submit_tag ''save'', >> :style=>''font-size:14px;font-weight:bold;width:100px'' %><br> >> <% end %> >> >> Any help is much appreciated. >> -- >> Posted via http://www.ruby-forum.com/. >> >> >> >> > > > -- > Rick R >-- Rick R --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Have you looked this tutorial
http://www.buildingwebapps.com/podcasts/6789-resources-page-links-categories-and-habtm/show_notes(download
the second link source code.)
Again, I''m new, so take this for what its worth...
Notice in their create method for pages and links (the two that have the
habtm relationship) there is no need to add the collection to the main
object. It looks like you shouldn''t need:
@recipe.category_recipe_ids[]<<(params[:category_recipe_ids])
You should just be able to use
@recipe = Recipe.new
And it should pull in the selected categories from your select options.
Not sure if it matters but in the example posted they are using a different
select tag than you''re using:
 <%= f.collection_select :category_ids, Category.find(:all, :order =>
''title''), :id, :title, {}, :multiple => true %>
Actually looking at your select tag I''m not sure how something like
"recipe_ids" would be set the way you have it:
<%= select_tag ''category_recipe_ids[]'',
options_from_collection_for_select(@categories, :id, :title), {
:multiple => true, :size =>5, :id => "categories_recipes_id"
}
%>
Why not try something like have shown:
 <%= f.collection_select :category_ids, Recipe.find(:all), :id, :title, {},
:multiple => true %>
Also if you were going to be setting the categories in recipes (which I
don''t think you need to do anyway), wouldn''t it be something
like
 @recipe.category_ids <<(params[:category_ids])
I don''t think you''d set the category_recipe_id ? (Again,
I''m new so I could
be way off here.)
On Wed, Jul 15, 2009 at 6:12 AM, Rails List <
rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
>
> I know it has been asked many times, but i could not find any working
> examples even after 2 days for googling. So, i thought let me ask here.
> How do I save in HABTM
>
> Its a simple application where users should be able to post recipes and
> that recipe HABTM categories. Multiple categories are selected through
> multi select list box.
>
> 1. I have 2 models: Recipe and Category
> 2. I have one join table: categories_recipes (category_id, recipe_id)
>
> Now this is in my controller:
>
> class RecipesController < ApplicationController
>  def new
>    @recipe = Recipe.new
>    @categories = Category.find(:all)
>  end
>
>  def create
>    @recipe = Recipe.new(params[:recipe])
>    @recipe.category_recipe_ids[]<<(params[:category_recipe_ids])
>    @recipe.save
>    redirect_to :action=>''show'', :id=>@recipe.id
>  end
>
>  def show
>    @recipe=Recipe.find(params[:id])
>  end
> end
>
> My new.html.erb
>
> <% form_for :recipe, @recipe, :url => { :action =>
"create" } do |f| %>
>  <%= f.text_field :title, :maxlength => 100 %><br>
>  <%= f.text_area :description, :rows=>''xx'',
:cols=>''yy'' %><br>
> <%= select_tag ''category_recipe_ids[]'',
> options_from_collection_for_select(@categories, :id, :title), {
> :multiple => true, :size =>5, :id =>
"categories_recipes_id" } %></p>
>  <%= submit_tag ''save'',
> :style=>''font-size:14px;font-weight:bold;width:100px''
%><br>
> <% end %>
>
> Any help is much appreciated.
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
-- 
Rick R
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Riiiick, you are a star. Atlast we have a working example for HABTM.  I 
have given here the complete code if anyone needs it.
Problem: A recipe can have many categories and a Category can have many 
recipes.
Model: Recipe
class Recipe < ActiveRecord::Base
  has_and_belongs_to_many :categories
end
Model: Category
class Category < ActiveRecord::Base
  has_and_belongs_to_many :recipes
end
Controller: recipes_controller
class RecipesController < ApplicationController
  def new
    @recipe = Recipe.new
    @categories = Category.find(:all)
  end
  def create
    @recipe = Recipe.new(params[:recipe])
    @recipe.save
    redirect_to :action=>''show'', :id=>@recipe.id
  end
  def show
    @recipe=Recipe.find(params[:id])
  end
end
View: New.html.erb
<% form_for :recipe, @recipe, :url => { :action => "create" }
do |f| %>
  <%= f.text_field :title, :maxlength => 100 %>
  <%= f.collection_select :category_ids, @categories, :id, :title, {}, 
:multiple => true %>
  <%= submit_tag ''save'', 
:style=>''font-size:14px;font-weight:bold;width:100px''
%><br>
<% end %>
View: Show.html.erb
<%= @recipe.title %>
<% @recipe.categories.each do |c|%>
   <%= c.title %>
<% end %>
-- 
Posted via http://www.ruby-forum.com/.