Welcome Guest, Not a member yet? Register   Sign In
Default variables
#1

[eluser]bigdaddysheikh[/eluser]
Hey guys,

I was talking to a friend about Ci this weekend(yes a nerd I am Smile ) and he was talking about the beauty of Ruby as he is a ruby programmer.

One thing he mentioned is that ruby carries default values across pages to make it a smart system.

For example,
If am editing item 1, then the page will be listed edit.php and will have information regarding that value.

But instead of putting a hidden form tag to hold ID value of 1, Ruby already stores that value in a variable called param. So when you go to update the photo, you can just get that value.

Another example,

If there is a image listing with each link having "index.php/1/imagename", then CI can retrieve that information getting the uri segment of 1. In ruby, the value of "1" is carried in some variable rather than the url. So instead you do not need the ID in the url.

This is an interesting concept and it makes sense considering the machine should already know where we are coming from rather than re-iterating it.

Just want to know if there is something like this in CI.
#2

[eluser]m4rw3r[/eluser]
You can save it in the session data, but it has to be done manually (I don't know if there is any other good solution).

But isn't this ruining the "stateless" way of the links?
Because if you bookmark it, the "1" will not be there, and what image should be shown?
#3

[eluser]Randy Casburn[/eluser]
bigdaddysheikh - Hi, the short answer is "yes, of-course". Any web scripting capability can provide the very same solutions. Some ASP .NET Frameworks do this very same thing as well. It is true they all implement these capabilities in slightly different ways, they all rely on the same basic technology to accomplish the goal.

Now the long answer and the "how" part:

I would like to set the stage properly for the context of this conversation before answering some of your questions directly.

First, for A non-Ruby, non-PHP context. Most of us are not working with systems where you have installed a software component at the client location that you control via an established synchronous link. An example of this would be a Java server linked up with a full Java client application or maybe a Java applet running inside a browser. These applications are able to maintain state between the client and server through the synchronous connection they have established. So state management has always been "built-in" for these applications.

Now those of us using CI or Ruby don't have the luxury of this synchronous link to the client. We have to create that magic. Both CI and Ruby (RoR specifically) do this in exactly the same way using the exactly the same technology. (As does .NET that I mentioned earlier BTW). I think what you'll find as big differences are the fact that RoR will "give you defaults" in order to tell you how you must accomplish something specifically and CI will only provide the "framework" for you to construct "your own method for accomplishing your task". So let me answer your direct questions.

Quote:But instead of putting a hidden form tag to hold ID value of 1, Ruby already stores that value in a variable called param. So when you go to update the photo, you can just get that value.

To correct this statement just a little bit..if you don't mind, "if you don't use RoR Active Record" you must use a hidden form field to pass state information. Here is the source reference if you'd like to look yourself: http://github.com/rails/rails/tree/maste..._helper.rb. (Search the page for hidden.) So in this regard, CI and RoR work exactly the same way. Both RoR and CI also use cookies and sessions to maintain state between page loads. In this sense they work the same way too. RoR provides more options for storing the session data.

Quote:If there is a image listing with each link having “index.php/1/imagename”, then CI can retrieve that information getting the uri segment of 1. In ruby, the value of “1” is carried in some variable rather than the url. So instead you do not need the ID in the url.

This is an interesting concept and it makes sense considering the machine should already know where we are coming from rather than re-iterating it.

Right. In RoR you must construct your "routes" properly in order for this "feature" to work properly. A couple of routes in RoR look like this:
Code:
(this is the default route from /config/routes.rb in your ruby install)
map.connect ':controller/:action/:id'
(and another sample)
map.connect '', :controller => "page", :action => 'index'
(source http://github.com/rails/rails/tree/maste...ertions.rb)

In CI we accomplish the same thing using the same term..Routes. You can find that in the documentation. Both systems are affected equally by Apache's mod_rewrite capability as well.

So using a combination of CI Routes and Sessions and possibly POST data and/or cookies you can do exactly what you'd like to do with your application.

I think what you'll find is that with CI as a framework, there is no command line tool "Gems" that will "rake" it all together for you. ;-) In other words, you have to write all yourself.

I hope this answers your questions.

Randy
#4

[eluser]Pascal Kriete[/eluser]
Randy answered your question, but I do want to clarify the routes part a bit.

In RoR, params is a hash (associative array) constructed of POST, GET, and URL variables. Basically anything that is passed along with the request.

For the POST and GET parameters this is pretty straightforward. For example a GET request like this:
/some/thing?animal=Fox&color=Blue
Will give you this hash: { "animal" => "Fox", "color" => "Blue" }

Easy enough, but how can you do it with URLs? The answer, as Randy already mentioned, is routes.
By default the hash keys :controller, :action, and :id are available (as well as all POST and GET variables). Those are defined by the default route:
Code:
// Default
map.connect ':controller/:action/:id'

Anything prefixed with a colon will be added to the params hash. So if I called example.com/some/thing/do, params[:controller] would give me "some".
By adding custom routes you can specify your own hash keys, to make them more meaningful.
Code:
map.download 'download/:file/immediately', :controller => 'assets', :action => 'download'
That will route example.com/download/somefilename to the download method of the assets controller and put "somefilename" in params[:file].

While we're at it. You'll notice that instead of map.connect, I used map.download . That is one of those beautiful RoR features known as named routes. If I want to create a new download link now, I can use:
Code:
#link_to is like the CI anchor function
link_to "Grab this file", download_url(:file => "myfilename")

And it creates the proper url pointing to "/download/myfilename/immediately".

That's my RoR lesson for today. Anyone who ports named routes to CI gets a cookie.
#5

[eluser]Randy Casburn[/eluser]
Thanks for the clarity inparo. You're the man!
#6

[eluser]bigdaddysheikh[/eluser]
Both excellent responses, I have to read a bit further in order to fully understand routes as I am new to CI. I have taken a slight look into Ruby and have been considering making a switch to using it instead of PHP. Actually ROR. But I want to give CI a good shot and see how far I can go with CI development.

Thank you guys once again.
#7

[eluser]Randy Casburn[/eluser]
Just remember, no matter which way you end up going, the grass will "appear" to be greener "in the other direction" for one reason or another. It just depends on who you speak with, what experience you have, and what you are trying to accomplish. Both have +'s and -'s

Best of luck to you,

Randy
#8

[eluser]bigdaddysheikh[/eluser]
Hey,

You are right. This friend of mine has been talking me into Ruby for a long time now and I think it would be an ignorant decision to not try it. So I see it the way you do. Both have +/-

Thanks man.
#9

[eluser]Randy Casburn[/eluser]
See you when you get back ;-) hehehehe




Theme © iAndrew 2016 - Forum software by © MyBB