I know this may be a very basic question, but confusing me.
If I write the following for example in the controller:
@x = App.new(params[:x])
I know that I'm trying to create a new object here, but how can I read
the params[:x] part.
I know also that "params" is a Hash table in the controller, but what is
the following statement trying to say when passing [:x] to params? What
is this value?
From where will I retrieve it?
I know this may be a very basic question, but confusing me.
If I write the following for example in the controller:
@x = App.new(params[:x])
I know that I'm trying to create a new object here, but how can I read
the params[:x] part.
I know also that "params" is a Hash table in the controller, but what is
the following statement trying to say when passing [:x] to params? What
is this value?
From where will I retrieve it?
Do you understand how hashes (not "Hash tables"!) work in Ruby? If not,
go read the Pickaxe book or other basic reference, then come back if you
have further questions.
Or are you asking where the params hash gets assigned to in the first
place? If so, then that's done by the Rails framework. I believe the
Rails Guides will tell you more about that.
Create a new App object, passing params[:x] to the initialize method.
As Marnen said this is very basic Ruby. Get the Pickaxe book and work
through it.
_Please_ go and read up on Ruby and go through some basic Ruby
tutorials. Then you will look back on the question you asked and be
embarrassed that every time someone googles and comes across it they
will think how silly it is.
The last thing is, will the value be passed as a parameter to [:x] when
creating a new object?
--
Posted viahttp://www.ruby-forum.com/.
This is both Rails basics and Ruby basics. 'params' is a hash and acts
like a hash. Here is a quick example from the console that I hope
helps but you would actually save time and learn more by reading up on
the subject:
params = {}
=> {}
params.class
=> Hash
params[:x] = 'SW Engineer'
=> "SW Engineer"
params.inspect
=> "{:x=>\"SW Engineer\"}"
puts params[:x]
SW Engineer
As you can see ':x' is the key to the value 'SW Engineer'. In Rails
that value would be coming from a field in your page called 'x'.