开发者

How do I produce an external URL as part of a replace_html call in Ruby on Rails?

开发者 https://www.devze.com 2022-12-31 11:39 出处:网络
Basically, I am attempting to render an external website (the url of which is stored in the database) into a page in my Ruby on Rails app.

Basically, I am attempting to render an external website (the url of which is stored in the database) into a page in my Ruby on Rails app.

I have a field in my model 'search' called 'search' that contains web addresses with the for开发者_JS百科m 'www.example.com' or 'example.com'. I am trying to use a link_to_function call with replace_html to replace the 'maincontent' div with an iframe tag using the value of 'search' in the current instance as the src for the tag.

My current attempt is the very ugly code below. I'd be grateful for either of the following types of responses:

  1. How can I rewrite the concatenation string to work correctly?
  2. How can I get the same effect (replacing the current content of the "mainContent" div with an iframe tag using a different method?

(I had to modify the code before to remove the <> from the iframe)

link_to_function h(search.title) do |page|

page.replace_html 'mainContent', 'iframe id="embedded" src="http://" + #{search.search} />' 

end 


  1. In ruby, #{variable} is interpolated if it's contained within double quotes.

    "<iframe id='embedded' src='http://#{search.search}' />"
    # Will expand to:
    "<iframe id='embedded' src='http://www.example.com' />"
    
  2. What's happening at the moment? Are you getting an empty iframe?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号