Flash Message Problem need solutions

How about something like this? Untested, but uses a self-referential join

== controller ==

def copy    artist = Artist.find(params[:id])    copy = artist.copy    flash[:message] = "This is the copy of #{@original.copies.size}"   redirect .... end

== model ==

class Artist < ActiveRecord::Base

  has_many :copies, :class_name => 'Artist', :foreign_key => 'copy_id'

  def copy    artist = copies.build_copy(attributes)    artist.save!    artist end


If you can paste your controller to http://pastie.caboo.se/ I will take a look. The cause of the error is not jumping out at me.

Cheers, Nicholas

It helps to provide a link to the code :slight_smile: It's ok though I managed to find it. The problem is:

  def copy           @artist = Artist.find(params[:id])             copy = artist.copy       flash[:message] = "This is the copy of #{@original.copies.size}"     end

The line should should be

copy = @artist.copy

you are missing the instance variable. You could get away with just this as you don't need instance variables.

  def copy       artist = Artist.find(params[:id])       copy = artist.copy       flash[:message] = "This is the copy of #{artist.copies.size}"       redirect_to artist_path(copy) # action => 'show', id => copy.id or maybe redirect back to the original?   end

Note the correction to the flash message. Also note that you will that you will probably want to do a redirect since this is not an idempotent action.

HTH, Nicholas

The solution I provided yesterday would have fixed this problem :slight_smile:

Here is the fix:


Sorry, I gave you the wrong syntax.

The code:


should be:


HTH, Nicholas


Just change:

flash[:notice] = 'This is the copy of #{@artist.copies.size}'


flash[:notice] = "This is the copy of #{@artist.copies.size}"

You need to use double quotes to evaluate the embedded expression.

HTH, Nicholas

Nish, are you going back to the original screen that you made a copy from, or a you making a copy from the new one - that's why it would be showing a size of one.

Cheers, Nicholas

You're very welcome, I am pleased we got you on the right track :slight_smile: