Commit a4cc1551

mo k <mo@mokhan.ca>
2011-08-03 03:09:34
add checkboxes for each category when uploading a new creation, and update the creation update page.
1 parent 57ce07b
app/controllers/creations_controller.rb
@@ -42,7 +42,7 @@ class CreationsController < ApplicationController
   # POST /creations.xml
   def create
     @creation = current_user.creations.create(params[:creation])
-
+    @creation.category_ids = params[:creation][:category_ids] ||= []
     respond_to do |format|
       if @creation.save
         format.html { redirect_to(@creation, :notice => 'Creation was successfully created.') }
@@ -58,6 +58,7 @@ class CreationsController < ApplicationController
   # PUT /creations/1.xml
   def update
     @creation = current_user.creations.find(params[:id])
+    @creation.category_ids = params[:creation][:category_ids] ||= []
 
     respond_to do |format|
       if @creation.update_attributes(params[:creation])
app/models/category.rb
@@ -1,3 +1,3 @@
 class Category < ActiveRecord::Base
-  has_and_belongs_to_many :creations
+  has_and_belongs_to_many :creations, :join_table => 'creations_categories'
 end
app/models/creation.rb
@@ -3,7 +3,7 @@ class Creation < ActiveRecord::Base
   validates :image,  :presence => true
   attr_accessible :user_id, :story, :name, :image, :remote_image_url
   belongs_to :user
-  has_and_belongs_to_many :categories
+  has_and_belongs_to_many :categories, :join_table => 'creations_categories', :uniq => true, :autosave => true
   mount_uploader :image, ImageUploader
 
   define_index do
app/views/creations/_form.html.erb
@@ -29,6 +29,12 @@
         </td>
       </tr>
     </table>
+    <% for category in Category.all %>
+    <div>
+      <%= check_box_tag "creation[category_ids][]", category.id, @creation.categories.include?(category) %>
+      <%= category.name %>
+    </div>
+    <% end %>
     <%= f.label :story %>
     <%= f.text_area :story, :class => "forms"  %>
     <input type="image" id="submit" name="submit" src="/images/template/button_submit.gif" title="Submit Message" />
app/views/creations/show.html.erb
@@ -25,6 +25,10 @@
     <% if @creation.user == current_user %>
       <%= button_to 'Delete', @creation, :method => :delete %>
     <% end %>
+    <p>categories</p>
+    <% @creation.categories.each do |category| %>
+      <p><%= category.name %></p>
+    <% end %>
 </div>
 
 <hr />