how would I persist a variable in the flash RAM across a request
coming from a form submission? ie. I have a form that submits to an
action that is checking for a particular key in the request context...
now from the form action I want to persist a value that's already in
the rc. How would I do this without using a hidden form field...? Is
it possible?
what Im doing now is, in my view checking if a value exists in the rc,
and if so, then set a hidden form field. I'd like to do this all
transparent to the user (experienced)...
To persist a request collection variable in your view without using a
hidden form field, I've used the persistVariables() method. The docs
suggest that it's only available in your event handlers, but I've
found you can use it in your views as well. So in your case, you can
put this code at the top of your view:
It doesn't explicitly say where the method can be called from, but the
code example given for the persistVariables() method was one that
you'd use in a handler (presumably -- since it's followed by a
setNextRoute call):
<cfset persistVariables('myObject,myArray,myString')>
<!--- Relocate with Confidence --->
<cfset setNextRoute('main/home')>
I'm not really sure where else the persistVariables() method is
mentioned in the docs. But it wasn't absolutely clear to me that I
could use it in a view until I experimented.
<!---Reroute back to edit artist page--->
<cfset setNextRoute("artist/rc.name/edit")>
</cffunction>
When i submit the form to the event handler i recieve the following
error message "Element NAME_ART is undefined in RC"...just wondering
what im doing wrong, thanks
I'm a little unclear as to what you're trying to do and exactly what
the flow is between your handlers and your views. Could you clarify:
1) Where you are originally setting your rc.name_art variable (you
said that it's in the URL, but it also looks like it's coming from a
form post).
2) After it's set, what handler is it being sent to and where it's
being routed from there.
3) Where are you putting you persistVariables() call?
4) From where are you dumping the requestCollection (a handler or
view)?
I'm sure once I'm clear on all this, i'll be able to help you out.
1) The rc.name_art variable is in the URL, but what I want to be able
to do is drag the variable from the URL and persist it to the next
event, instead of using a hidden form field.
2) The form is being posted to a function called Set Artist...shown
below....
<!---Edit Artist Action--->
<cffunction name="setArtist" output="false"
returntype="void">
<cfargument name="EVENT" type="any">
<!---request to reference collection--->
<cfset var rc = Event.getCollection()>
<!---Prepare logic--->
<cfset artist = createObject
('component',"myapp.model.mdl_artists")>
<cfset rc.qrySetArtist = artist.setArtistData
(nameArt=rc.name,oldNameArt=rc.name_art)>
<!---Reroute back to edit artist page--->
<cfset setNextRoute("artist/#rc.name#/edit")>
3) The persist variables call I am putting in the view which contains
the form....
<cfset persistVariables("name_art")>
4)Im not quite sure what you mean here....but my aim is for the
persistVariable(name_art) at the top of my view to drag its value to
the SetArtist method...so I can use it in my cfset
Sorry, but it's still not clear to me where the name_art variable is
being set in the request collection. You said that it's in the URL,
but from #2 in your post it still looks like rc.name and rc.name_art
are coming from your form (are you doing a form method="get"?).
Anyway, I think I see what's going on here. You're trying to persist
the variable in the wrong place. When you call the setNextRoute()
method in your setArtist() action, you're starting a new request,
which wipes the request collection clean. That's why you're getting
the message that the name_art variable doesn't exist in the rc once
you relocate back to your form.
Try this:
1) remove the peristVariables call in your view (or just comment it
out for now until you test it)
2) change your setNextRoute() call at the end of your setArtist()
function to this: setNextRoute(route="artist/#rc.name#/edit",
persist="name_art")
On my artist pages...for example ulr ="mysite/artist/amy+winehouse"
the artist name has been set up via the routes to represent the
variable name "name_art"
So if in my view "artist.cfm" If place this code #rc.name_art# I will
see amy winehouse
Now I have a form in this view which allows me to add songs for the
artist "in this case amy winehouse"...
The form submits to my handler "setArtist"..
For the setArtist function to work, it needs to know the artist name I
am submitting the song for, to insert it into the correct database
At the moment, the only way I can let the setArtist method know that I
am submitting the song for amy winehouse, is to include a hidden form
field on my form, like so
<!---Hidden Form Field---->
<input name="name_art"
type="hidden"
value="#rc.name_art#">
I would like to be able to remove this hidden form field, but somehow
be able to grab my artists name from the url, so I can pass it on to
my "setArtist" method...
I am currently doing a form method ="post" and rc.name and rc.name_art
are currently coming from the form...