small medium large xlarge

Generic-user-small
17 Nov 2008, 23:06
James West (104 posts)

<%= @users = User.find(:all, :order => "name" ) form.collection_select(:name, @users, :id, :name) %>

Hi, On page 507 there is an example (as shown above) of how to use collection_select as a means of associating linked tables when editing/updating a record

I have a category that belongs to catalogues and am trying to use this method to pick the catalogue that the category belongs to when creating/editing a category record.

I am totally unable to get this method to work in as outlined in the book. I get the selection list populated with the correct data but am getting the following error when submitting the form to create a new record.

NoMethodError in Categories#create

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.map

My view looks like this. (This is a rendered template(Not a partial) used for both editing and creating category records)

`

Enter Catalogue Details <p>
	    <%= f.label :catalogue_name -%>:
		<%= f.collection_select(:category_name, @catalogues, :id, :name)%>
  		</p>
  		<p>
	    <%= f.label :category_name -%>:
	    <%= f.text_field :category_name %>
	</p>
  		<p>
	    <%= f.label :category_description -%>:
	    <%= f.text_area :category_description, :rows => 2, :cols => 40 %>
	</p>
  		<p>
	    <%= f.label :category_image_url -%>:
	    <%= f.text_field :category_image_url %>
	</p> `

The relevant parts of my controller look like this

` # GET /categories/new # GET /categories/new.xml def new @category = Category.new @catalogues = Catalogue.get_all_catalogues respond_to do |format| format.html # new.html.erb format.xml { render :xml => @category } end end

# GET /categories/1/edit def edit @category = Category.find(params[:id]) @catalogues = Catalogue.get_all_catalogues end

# POST /categories # POST /categories.xml def create @category = Category.new(params[:category])

respond_to do |format|
  if @category.save
    flash[:notice] = 'Category was successfully created.'
    format.html { redirect_to(@category) }
    format.xml  { render :xml => @category, :status => :created, :location => @category }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
  end
end   end

# PUT /categories/1 # PUT /categories/1.xml def update @category = Category.find(params[:id])

respond_to do |format|
  if @category.update_attributes(params[:category])
    flash[:notice] = 'Category was successfully updated.'
    format.html { redirect_to(@category) }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
  end
end   end `

The category model looks like this and all validation passes

` class Category < ActiveRecord::Base belongs_to :catalogues has_many :products

validates_presence_of :category_name validates_uniqueness_of :category_name validates_presence_of :catalogue_id, :message => “No catalogue has been assigned!”

validates_format_of :category_image_url, :with => %r{.(gif|jpg|png)$}i, :unless => Proc.new{|c| c.category_image_url.blank?}, :message => ‘must be a URL for GIF, JPG ‘ + ‘or PNG image.’

end `

The catalogue model looks like this

` class Catalogue < ActiveRecord::Base has_many :categories validates_presence_of :name validates_uniqueness_of :name validates_format_of :thumbnail_url, :with => %r{.(gif|jpg|png)$}i, :unless => Proc.new{|c| c.thumbnail_url.blank?}, :message => ‘must be a URL for GIF, JPG ‘ + ‘or PNG image.’

def self.get_live_catalogues @catalogues = self.find_all_by_available_to_sell(true) end

def self.get_all_catalogues @catalogues = self.find(:all, :order => “name”) end end `

Is this a bug that needs reporting as errata or is it just me being an a**e and getting something wrong?

Thanks

James

Generic-user-small
18 Nov 2008, 00:08
James West (104 posts)

Ooops it was me being an arse :-)

<%= f.collection_select(:catalogue_id, @catalogues, :id, :name)%>

Is what was needed.

Sorry for the messy opening post. The code tags seem to have messed up the dislay somewhat!

You must be logged in to comment