views:

230

answers:

1

Preface: I consider myself "slightly effective" in ruby on rails, and a complete novice in javascript. Also, yes, I have installed jQuery and associated plugins instead of the default Prototype library.
I am in a situation where I am pulling in a table from off-site in an iframe (which is taking care of all internal JS for me) such that when a part of the table is clicked, a td will gain the class "active." What I would like to do is take this info (I'm assuming I can get it in a string format), and pass it to a method (in my controller, I'm assuming) which will parse the html, pull out the pertinent info, and then call a creation method in the same controller with the parsed info, the end result being a new item in that table.

What I have so far is javascript which I believe is correct:

<script type="text/javascript">
  var ImportInfo = function() {
  var info = $('td.active').html();
  // call controller action which parses the given string,
  //checks for existence in database, and adds new row if needed
}

$("#Import").click(ImportInfo);
</script>

and, of course, a button with id="Import."

I have looked at this question: http://stackoverflow.com/questions/1334447/using-jquery-to-call-a-controller-action but am somewhat unsure as to how to call a controller action to pass the contents of the td as a string. Is this doable with the jQuery post method?

ADDED INFO: my iframe:

<iframe id='locator' src="http://hosted.where2getit.com/wafflehouse/indexnew.html" width="740" height="700" marginheight="0" marginwidth="0" scrolling="no" frameborder="0" align="bottom" name="plg_iframe">No Frames</iframe>
+1  A: 

Too bad you dropped Prototype -- I could have helped you better ;-)

jQuery experts feel free to correct me but I believe you want something like:

<script type="text/javascript">
  var ImportInfo = function() {
  var info = $('td.active').html();
  // call controller action which parses the given string,
  //checks for existence in database, and adds new row if needed
  $.ajax({
    url: '/controller/action',
    data: { paramName: info }
  })
}

$("#Import").click(ImportInfo);
</script>

If you wanted to do it with Prototype, you would use:

<script type="text/javascript">
  var ImportInfo = function() {
  var info = $$('td.active')[0].innerHTML;
  // call controller action which parses the given string:
  new Ajax.Request('/controller/action',{
    method: 'post'
    parameters: { paramName: info }
  })
}

$("#Import").click(ImportInfo);
</script>

Now, ater re-reading your question, I see that the <td> you want to get data from is in an iFrame from a different site, is this correct? If so, JavaScript will have no access to that iFrame because of security restrictions. See this MSDN article or google for "Cross-domain iframe security". If this is your issue, please provide a lot more detail about the two domains in question... you may or may not be out of luck.

Josh
Heh, if you can help me out from the start with prototype, I'd be more than willing to switch back.This is the first (and probably only) bit of JS in the project, so it's not as if it's too many steps back. The only reason I switched is because the colleague I talked to recommended jQuery over Prototype.
Zind
@Zind: See my edited answer. Be aware that it sounds like what you're trying to do is impossible due to cross-domain iframe security
Josh
Yeah, I noticed that that might be a problem when I was fooling around in firebug just trying to see if what I had so far was even fetching the right data - I might actually be out of luck, but thanks for the JS assist anyway, it'll come in mighty handy should I end up able to cross the gap.
Zind
Post some more information about the issue, some details about the iFrame, and I will revise my answer.
Josh
I added my iFrame, it should have most of the info. I was messing around in firebug and was able to see the iframe, and its html, but none of the html of the source, as predicted.
Zind
@Zind: What domain is your Rails app running on? Do you control the content at hosted.where2getit.com?
Josh
No, no I do not, I'm at hostingrails.com and the where2getit is just a public widget. Since I'm in a bit of a time-crunch this piece is going on the backburner for now, so I will not be focusing on it as much. However, what has been suggested to me is actually somehow downloading/caching the html generated by the widget server-side, and serving it to the client from there.
Zind
@Zind: That was going to be exactly my answer. If you write a proxy on hostingrails which fetches the content from where2getit, and you point your iFrame at hostingrails.com/your_proxy, then you JavaScript can read the DOM of the iFrame and the code above will work. Good luck!
Josh