mtcrts70
mtcrts70

Reputation: 35

Rails and embedded ruby

For my website I have the following title helper:

<% provide(:title, 'My Page') %>

Which produces something like this:

enter image description here

Instead of the generic 'My Page' I'm trying to add the user name, for instance 'mtcrts70'. To do this I tried the following code:

<% provide(:title, '<%= current_user.email[/[^@]+/] %>') %>

Note that <%= current_user.email[/[^@]+/] %> renders perfectly when outside of the <% %> tags.

It seems like the issue is putting <%= %> inside of <% %>. Is there a way to get around this?

Error message:

SyntaxError in Static_pages#home

Showing /home/matt/Documents/Ruby/rails_projects/ninja_speak_app/app/views/static_pages/home.html.erb where line #4 raised:

/home/matt/Documents/Ruby/rails_projects/ninja_speak_app/app/views/static_pages/home.html.erb:4: syntax error, unexpected $undefined, expecting ')'
... ;@output_buffer.safe_concat('\') %>
...                               ^
/home/matt/Documents/Ruby/rails_projects/ninja_speak_app/app/views/static_pages/home.html.erb:15: syntax error, unexpected ')', expecting keyword_end
       ');@output_buffer.append= ( cur...
         ^

Upvotes: 1

Views: 42

Answers (1)

Arie Xiao
Arie Xiao

Reputation: 14082

What's inside <% %> is a piece of normal Ruby code. You just need to pass in the value as argument to the provide helper method.

<% provide(:title, current_user.email[/[^@]+/]) %>

Upvotes: 3

Related Questions