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.

On Mon, Sep 20, 2010 at 2:39 PM, Abder-Rahman A. [email protected]
wrote:

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’”.


Greg D.
destiney.com | gregdonald.com

Thanks for your reply @Greg.

Regarding this part: params[:photo][‘photo’]

What is [‘photo’]?

Thanks.

On Mon, Sep 20, 2010 at 2:48 PM, Abder-Rahman A. [email protected]
wrote:

Regarding this part: params[:photo][‘photo’]

What is [‘photo’]?

It’s probably from a file field:


Greg D.
destiney.com | gregdonald.com

On Mon, Sep 20, 2010 at 2:56 PM, Abder-Rahman A. [email protected]
wrote:

“photo” is originally a column field, but what does it mean when
inserting it in this shape?

params[:photo][‘photo’]

If params[:photo] is telling us that we have a key :photo, what is the
use of [‘photo’], this is what I’m not getting.

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

params[:photo]

params[:photo][‘photo’]


Greg D.
destiney.com | gregdonald.com

Greg D. wrote:

On Mon, Sep 20, 2010 at 2:48 PM, Abder-Rahman A. [email protected]
wrote:

Regarding this part: params[:photo][‘photo’]

What is [‘photo’]?

It’s probably from a file field:


Greg D.
destiney.com | gregdonald.com

“photo” is originally a column field, but what does it mean when
inserting it in this shape?

params[:photo][‘photo’]

If params[:photo] is telling us that we have a key :photo, what is the
use of [‘photo’], this is what I’m not getting.

Thanks.

Just to make it more clear. When we say:

params[:photo]

This looks like follows in the “params” table:


Key | Value

:photo | “xyz”

How can this be represented in the params table?

params[:photo][‘photo’]

Thanks.

I see, thanks a lot.

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”

On Mon, Sep 20, 2010 at 4:12 PM, radhames brito [email protected]
wrote:

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

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 D.
destiney.com | gregdonald.com

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.

Thanks @radhames.

But, still, I cannot get the idea of:

params[:photo][‘photo’]

And how it is represented.

Can you clarify on this?

Thanks.

@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?

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

Abder-Rahman A. 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,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

dude , that some jurasick code, if from 2007?

wow, what the guy is down is checking the myme
typehttp://www.iana.org/assignments/media-types/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 http://www.iana.org/assignments/media-types/application/

audio http://www.iana.org/assignments/media-types/audio/

example http://www.iana.org/assignments/media-types/examples/

image http://www.iana.org/assignments/media-types/image/

message http://www.iana.org/assignments/media-types/message/

model http://www.iana.org/assignments/media-types/model/

multipart http://www.iana.org/assignments/media-types/multipart/

text http://www.iana.org/assignments/media-types/text/
video http://www.iana.org/assignments/media-types/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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs