# Embedded expressions in ERB

**URL:** <https://www.ruby-forum.com/t/embedded-expressions-in-erb/184597>\
**Category:** Rails\
**Created:** [March 5, 2010, 1:27am UTC](https://www.ruby-forum.com/t/embedded-expressions-in-erb/184597 "2010-03-05T01:27:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![allanium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@allanium](https://www.ruby-forum.com/u/allanium)\
**Post date:** [March 5, 2010, 1:27am UTC](https://www.ruby-forum.com/t/embedded-expressions-in-erb/184597/1 "2010-03-05T01:27:47Z")

</div>

I’m stuck in trying to get an embedded expression in ERB.

I’m trying to put @post into @httppost. However when I print @httppost I  
get (notice the @post):  
{“aps”: {“badge”: 1, “alert”, #{@post}}, “sound”: “cat.caf”,  
“device\_tokens”:  
[“455ab1b23cbfd97b7d2ef90a49d222e40f4c6c9b2570e84ff94fe4afeea12345”]}

It should say:  
{“aps”: {“badge”: 1, “alert”, “This is a test”}, “sound”: “cat.caf”,  
“device\_tokens”:  
[“455ab1b23cbfd97b7d2ef90a49d222e40f4c6c9b2570e84ff94fe4afeea12345”]}

I put everything in my view to make it easier to troubleshoot:

\<% @post = params[:post] %\>

post params: \<%= @post %\>

  

\<% @httppost = ‘{“aps”: {“badge”: 1, “alert”, “#{@post}”}, “sound”:  
“cat.caf”, “device\_tokens”:  
[“455ab1b23cbfd97b7d2ef90a49d222e40f4c6c9b2570e84ff94fe4afeea12345”]}’  
%\>

\<%= @httppost %\>

You can see the enclosed screen capture to see what my results are.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![allanium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@allanium](https://www.ruby-forum.com/u/allanium)\
**Post date:** [March 5, 2010, 1:35am UTC](https://www.ruby-forum.com/t/embedded-expressions-in-erb/184597/2 "2010-03-05T01:35:51Z")

</div>

On Mar 4, 2010, at 4:27 PM, Allan L. wrote:

> It should say:  
> post params: \<%= @post %\>
> 
>   
> 
> \<% @httppost = ‘{“aps”: {“badge”: 1, “alert”, “#{@post}”}, “sound”:  
> “cat.caf”, “device\_tokens”:  
> [“455ab1b23cbfd97b7d2ef90a49d222e40f4c6c9b2570e84ff94fe4afeea12345”]}’  
> %\>

You’ve got it wrapped in single quotes… which won’t interpolate.  
You want double quotes… or in your case since you’ve got a lot of  
double quotes you probably don’t want to escape… use the %Q method…

\<% @httppost = %Q!{“aps”: {“badge”: 1, “alert”, “#{@post}”}, “sound”:  
“cat.caf”, “device\_tokens”:  
[“455ab1b23cbfd97b7d2ef90a49d222e40f4c6c9b2570e84ff94fe4afeea12345”]}!

The ‘!’ can be whatever you want (I think) so pick something that  
isn’t in your string. If it’s paired (ie %Q{…}) use the opening/  
closing keys.

-philip

---

<div class="post-metadata">

**Author:** ![allanium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@allanium](https://www.ruby-forum.com/u/allanium)\
**Post date:** [March 5, 2010, 4:33am UTC](https://www.ruby-forum.com/t/embedded-expressions-in-erb/184597/3 "2010-03-05T04:33:27Z")

</div>

Philip,

Thanks a lot! This really helped me!

-A

Philip H. wrote:

> On Mar 4, 2010, at 4:27 PM, Allan L. wrote:
> 
> > It should say:  
> > post params: \<%= @post %\>
> > 
> >   
> > 
> > \<% @httppost = ‘{“aps”: {“badge”: 1, “alert”, “#{@post}”}, “sound”:  
> > “cat.caf”, “device\_tokens”:  
> > [“455ab1b23cbfd97b7d2ef90a49d222e40f4c6c9b2570e84ff94fe4afeea12345”]}’  
> > %\>
> 
> You’ve got it wrapped in single quotes… which won’t interpolate.  
> You want double quotes… or in your case since you’ve got a lot of  
> double quotes you probably don’t want to escape… use the %Q method…
> 
> \<% @httppost = %Q!{“aps”: {“badge”: 1, “alert”, “#{@post}”}, “sound”:  
> “cat.caf”, “device\_tokens”:  
> [“455ab1b23cbfd97b7d2ef90a49d222e40f4c6c9b2570e84ff94fe4afeea12345”]}!
> 
> The ‘!’ can be whatever you want (I think) so pick something that  
> isn’t in your string. If it’s paired (ie %Q{…}) use the opening/  
> closing keys.
> 
> -philip
