11 Aug 2008, 09:50
Generic-user-small

Tan YL (19 posts)

hi,

need some help again.

i want to insert ruby code into a string like this: <%= image_tag(“/image/groceries/<%=h groceries.id %>.jpg”, :class =>

how do i call the model in this case?

11 Aug 2008, 10:14
Samr_small_pragsmall

Sam Ruby (584 posts)

<%= image_tag(”/image/groceries/#{h groceries.id}.jpg”, :class => ...

In beta 1.3, see the description for expression interpolation on page 650.

11 Aug 2008, 10:20
Generic-user-small

Tan YL (19 posts)

thanks. btw which is more preferable groceries.id.to_s or #{h groceries.id}

11 Aug 2008, 10:44
Samr_small_pragsmall

Sam Ruby (584 posts)

Expression interpolation calls @to_s@ for you, so that’s not necessary in this case.

The @h@ function does something different, it escapes characters which might be mistaken for markup. id’s are typically numbers, so such a function is not required here.

If you are like me and need to establish and ingrain in habits in order to avoid careless mistakes, I would actually suggest:

"/image/groceries/#{h(groceries.id)}"

If you are the type that feels confident that you know what you are doing, you might prefer the simpler:

"/image/groceries/#{groceries.id}"

And somebody who really knows what they were doing would use named routes instead (page 417 in beta 1.3).

  You must be logged in to comment