1

I have a model Resources that when a new entry is made I want to be able to assign a category to it. Code is setup as so:

Resource.rb

has_many :categories

category.rb

has_many_and_belongs_to :resources

(resources)_form.html.erb (along with the rest of resources fields)

<%= form_for @resource do |f| %>
  <%= f.select :category, Category.all, :prompt => "Category" %>
<%= end %>

categories_controller.rb

class CategoriesController < ApplicationController
    def new
        @category = Category.new
    end

    def edit
        @category = Category.find(params[:id])
    end

    def create
      @category = Category.new(category_params)

      @category.save
      #redirect_to @category
    end

    def update
      @category = Category.find(params[:id])

      #if @category.update(params[:category].permit(:category_params))
      #  redirect_to @category
      #else
      #  render 'edit'
      #end
    end

    def destroy
      @category = Category.find(params[:id])
      @category.destroy

      #redirect_to categorys_path
    end

    private
      def category_params
        params.require(:category).permit(:name)
      end 
end

There will only be 4 or so categories and they won't change. I only need to be able to assign a category via dropdown when creating a resources entry, and list entries under a certain category.

right now, I get the error "undefined method 'category'" when trying to view resources/new.

Any easy to understand (rails beginner) help is appreciated

2 Answers2

1

First on your resource model you need also a has_and_belongs_to_many

For the input you need to add the param multiple: true

See the answers on this question for more info Rails 3: Multiple Select with has_many through associations

And you need to use categories on the form and not category.

Community
  • 1
  • 1
Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
  • I changed both models to has_and_belongs_to_many and added multiple: true to the form input but still get the same error. –  Mar 06 '14 at 00:15
0

You want ActsAsTaggableOn!

https://github.com/mbleigh/acts-as-taggable-on

This brilliant gem solves the category problem for you. However, that isn't the question you are asking so... You are getting the error because you don't have your associations set up correctly.

  • First problem, you need to be using has_and_belongs_to_many on both models.
  • Second problem, you need to change your select field to :categories
  • Third problem, you need to have multiple: true in the form to allow multiple selections.

<%= f.collection_select :category_ids, Category.all, :id, :name, :prompt => "Category", :multiple => true %>

I highly recommend checking out the ActsAsTaggableOn because the documentation is amazing. There is also a RailsCast for it.

Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62
  • Switching to f.select :categories, I now get the error `PG::UndefinedTable: ERROR: relation "categories_resources" does not exist` –  Mar 06 '14 at 00:29
  • Yup, you need to have that table to store the relationships. You won't have a model for it though. If you want a model, then you will want to use the `has_many :through` associations instead. – Sixty4Bit Mar 06 '14 at 02:22
  • Be sure to read the Associations Guide: http://guides.rubyonrails.org/association_basics.html – Sixty4Bit Mar 06 '14 at 02:24
  • Whether I did this correctly I'm not sure. I created a new model categories_resources that holds the id of both categories_id and resources_id. This got the error fixed and the dropdown appears, but the selection options look like code <#category:0272361192>. How do I get this to show the name in text. And did I do everything correctly? Thanks for the help. –  Mar 06 '14 at 06:33
  • Updated to use collection_select which allows you to specify which properties will be used in the drop down. Make sure that :categories is permitted in your controller: http://stackoverflow.com/questions/17734218/multiple-select-issue-with-a-habtm-relationship-using-rails-4 – Sixty4Bit Mar 06 '14 at 23:58