unless params[:photo]['photo'].content_type =~ /^image/

In the "Rails cookbook", I read the following statement:

unless params[:photo]['photo'].content_type =~ /^image/

What do those parts mean?

* params[:photo]['photo']?

* =~ /^image/

Thanks.

In the "Rails cookbook", I read the following statement:

unless params[:photo]['photo'].content_type =~ /^image/

What do those parts mean?

It means you need to learn Ruby.

* params[:photo]['photo']?

params is a hash. :photo is a key in the params hash.

* =~ /^image/

=~ means match, as in regular expression matching.

/^image/ is a regular expression, it means "starts with 'image'".

Thanks for your reply @Greg.

Regarding this part: params[:photo]['photo']

What is ['photo']?

Thanks.

It's probably from a file field:

<input type="file" name="photo[photo]" />

Greg Donald wrote:

It means there's another hash inside the params[:photo] hash.

params[:photo] <input type="file" name="photo" />

params[:photo]['photo'] <input type="file" name="photo[photo]" />

I see, thanks a lot.

Just to make it more clear. When we say:

params[:photo]

This looks like follows in the "params" table:

functions that return booleans( true or false), can end with a question mark ( ? )

and in ruby what is called a hash is a list of key value pairs, that is

:name => “abder”, :id=> ‘3’

that is a hash, if i want the value of name , call it like this

:name

that returns “abder”, and hashes can be nested, like this

:params=> { :name => “abder”, :id=> ‘3’}

if i cal params directly i get ,

:name => “abder”, :id=> ‘3’

but if i want a value that is nested i do

params[:name]

and it returns

“abder”

params[:photo]

This looks like follows in the “params” table:


Key | Value


:photo | “xyz”


not in the table in the params hash , when your app recives info back from the user rails arranges it in a hash called parasm

look at you console log when ever you create a resource or dear an specific one you will see

parameter => { :id=>1, :name=>“blah” … }

to read from those nested values you call them params[:id]

but that is how rails arrenges the request that has nothing to do with the table.

Any Ruby method name can end in a question mark, returning a boolean is not required.

ruby-1.9.2-p0 > def foo?; return 1; end; foo? => 1

@greg

inst that llike a convention, like if a method raises an exception the developer can end ti with a bang ( ! ). if a method returns boolean doesnt the developer end it with ? as a convention?

is it or not convention?

Thanks @radhames.

But, still, I cannot get the idea of:

params[:photo]['photo']

And how it is represented.

Can you clarify on this?

Thanks.

dont worry im reading the book right know to see what the code is about , ill reply in a few minutes

Abder-Rahman Ali wrote:

Thanks @radhames.

But, still, I cannot get the idea of:

params[:photo]['photo']

And how it is represented.

Can you clarify on this?

What part of that syntax don't you understand?

If the square brackets, review Hash syntax in Ruby.

If the nesting, read up on what Rails puts in the params hash.

Ask if you have further questions.

Thanks.

Best,

dude , that some jurasick code, if from 2007?

wow, what the guy is down is checking the myme type on the create action, today that is done in a validation in the model.

params[:photo][‘photo’]

is something like this

parameter => {:item=>{:name=>“blah blah”, :description=> " more blah blah"},:photo =>{:photo=>[ bunch of data about the file ] } }

at the end in that bunch of that about the file the browser pases the media type, that is somethie the browser does and they can be

application

audio

example

image

message

model

multipart

text

video

plus the images can be jpg or tiff

with .content_type =~ /^image/

he is saying grab the media type see if it starts with image, as the browser pases somthing like this

image/jpg

or

image/tiff

so with

unless params[:photo][‘photo’].content_type =~ /^image/ flash[:error] =" that is not an image file " render :action => ‘new’

end

he is checking from the params hash if the file uploaded has a media type of image. if not render new