开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜