Flash Message Problem need solutions

Hello, I am basically trying to figure out how to show a flash message
for my system. I have basically got a code in my controller that copies
records, this is the code:

def copy
@artist_to_copy = Artist.find(params[:id])
@artist = Artist.new(@artist_to_copy.attributes)
@artist.save
end

I want a flash message to appear so that everytime I copy a original
record it will show on the flash message that it is i.e “This is the
copy of 1” and if I copy the record again it will say “This is the copy
of 2” etc. I cant seem to figure out how this would work if there are
many orginal records like for instance 1, 2, or 3. How would the flash
message know that id 4 is a copy of 2 etc. All very confusing but I
would be grateful if someone can come up with a solution for me, many
thanks

Nish #hmm#

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

end

On Nov 24, 7:01 am, Nish P. [email protected]

I have tryed this and get this error message:

NameError in ArtistsController#copy

undefined local variable or method `artist’ for
#ArtistsController:0x481f0e8

RAILS_ROOT: C:/INSTAN~1/rails_apps/example4/config/…
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/controllers/artists_controller.rb:25:in `copy’

Request

Parameters: {“id”=>“79”}

Show session dump


flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

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

On Nov 24, 10:51 am, Nish P. [email protected]

Nicholas H. wrote:

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

On Nov 24, 10:51 am, Nish P. [email protected]

Sorry for the late reply had a holiday i have pasted my controller
thanks again for your help

Nicholas H. wrote:

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

On Nov 24, 10:51 am, Nish P. [email protected]

Sorry for the late reply had a holiday i have pasted my controller
thanks again for your help

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

On Nov 27, 4:30 am, Nish P. [email protected]

I know I am a newbie and always reading these books on Ruby, but I just
get frustrated when things dont work, lol I have another error messgae
and I am trying to figure out what it is i cant seem to create a copy, I
will send it as an attachment so you can look at it better, thanks for
you help again. You have been awesome!

Nish P. wrote:

I know I am a newbie and always reading these books on Ruby, but I just
get frustrated when things dont work, lol I have another error messgae
and I am trying to figure out what it is i cant seem to create a copy, I
will send it as an attachment so you can look at it better, thanks for
you help again. You have been awesome!

the error message is :

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.copy

RAILS_ROOT: C:/INSTAN~1/rails_apps/example4/config/…
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/controllers/artists_controller.rb:25:in `copy’

etc

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

Here is the fix:

http://pastie.caboo.se/123019

On Nov 28, 5:56 am, Nish P. [email protected]

Nicholas H. wrote:

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

Here is the fix:

Parked at Loopia

On Nov 28, 5:56 am, Nish P. [email protected]

many thanks again, this stupid things is never gonna work :frowning: I done what
you asked me to do and cmae up with this:

NoMethodError in ArtistsController#copy

undefined method `build_copy’ for Artist:Class
RAILS_ROOT: C:/INSTAN~1/rails_apps/example4/config/…

Sorry, I gave you the wrong syntax.

The code:

copies.build_copy(attributes)

should be:

copies.build(attributes)

HTH,
Nicholas

On Nov 28, 8:00 am, Nish P. [email protected]

I see!!! it works now thanks you very much Nicolas the only thing is
that the message doesnt show up i dont know why and when i change it to
a notice the only thing showing on the page is This is the copy of
#{@artist.copies.size}
as the message, strange

Nish:

Just change:

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

to

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

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

HTH,
Nicholas

On Nov 28, 9:09 am, Nish P. [email protected]

Many thanks Nicolas for your help you have helped me alot on this and i
cant thank you enough, going home now it has been a long day in the
office i will see what i can do tomorrow but once again thankyou my
friend!

Kind Regards

Nish

Yes Nicolas the flash message still falls over everytime the error

ActiveRecord::StatementInvalid in ArtistsController#copy

Mysql::Error: Unknown column ‘artists.copy_id’ in ‘where clause’: SELECT
count(*) AS count_all FROM artists WHERE (artists.copy_id = 112)

RAILS_ROOT: C:/INSTAN~1/rails_apps/example4/config/…

its doesnt like this does it:

flash[:message] = “This is the copy of #{@artist.copies.size}”

ok, i made another column on mysql called copy_id. Then the message
didnt fall but now it keeps on show this is the copy of 1, all the time,
so close!!! i looked at the database and it seems to be working fine, i
only need the differences of the keys and thats my answer!!!, going home
now

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

On Nov 28, 11:42 am, Nish P. [email protected]

Nicolas sorry i understand what you mean it works!!! thank you so much
for your help you have been brilliant, i cant believe how helpful these
forums can be, thankyou so much!!!

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

On Nov 29, 4:51 am, Nish P. [email protected]