简体   繁体   中英

Rails4 - How to pass image_tag as a parameter in Rails app?

I am trying to pass image tag as a parameter.

When I try to hardcode it like below, it works perfectly.

messages_controller.rb

class MessagesController < ApplicationController      

  def create
  .
  .
  Pusher['private-'+pushid.id.to_s].trigger('new_message', 
  {:subject => "You have received a new message from <b>John</b>. 
  <img class=\"img-rounded\" 
  src=\"http://mywebsite.com/images/mini/missing.png\"  />" })
  .
  end

end

I tried the following to make it dynamic but I get syntax errors. I am not able to pass image tag.

class MessagesController < ApplicationController      

  def create
  .
  .
  Pusher['private-'+pushid.id.to_s].trigger('new_message', 
  {:subject => "You have received a new message from <b>"+
  current_user.name.capitalize+"</b>." + 
  <%= image_tag(current_user.avatar.url(:mini)).gsub("\"", "'") %>  })
  .
  end

end

Could someone share how to do it correctly?
Kindly help. Thanks

This should work:

subject = "You have received a new message from "+
"<b>#{current_user.name.capitalize}</b>."+
"#{ActionController::Base.helpers.image_tag(current_user.avatar.url(:mini)).gsub('"', '\'')}"
Pusher['private-'+pushid.id.to_s].trigger('new_message',{:subject => subject})

Within a string you can use #{RUBY_CODE_HERE} to insert ruby.

Edit: I pulled the subject out as a variable and made the string multi-line just so it's easier to see within the width of the stackoverflow answer, you can of course keep it as one line in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM