views:

41

answers:

3

Here is what I want to do:

<%= link_to "Approve", brand_user_brand_roles_path(@brand),
    :with => "forms[0]['user_brand_role_role_id'].value", :method => "POST", :remote => true %>

I know that this doesn't work. I want to call a controller method remotely, including as a parameter a value that is selected in a field other than what is clicked.

How can I use Javascript to refer to a different field in a link_to? Or is there another strategy that is better?

+1  A: 

If you're using prototype or jQuery, something like so will access the value in a field:

$('#id-of-field').val() # or value() depending on your js

So, add that into your :with statement:

:with => "'role_id=' + $('#user_brand_role_role_id').value() +
 '&other_value=' + $('#id_of_other_thing').value()"

You can play around in the JS console to get the right output, then put it into your link_to statement.

wesgarrison
Wes I guess I'm more confused by where I can put the Javascript calls rather than the Javascript itself. Is ":with" my only option, because it does not appear to be a parameter for anything other than remote_function.
AKWF
First, rails 2 or rails 3? If you want to go all unobtrusive, put it in application.js, bind it to the link and grab the fields you want. I couldn't find :with anywhere in the rails 3 docs, but that doesn't mean it isn't there. I haven't used it there, though.
wesgarrison
Rails 3. I'm definitely trying to go all unobtrusive. I'm using JQuery. If I bind it to the link, I have to bind it by "id", right? And forgive my ignorance, but when you say put it in application.js, you mean write the actual Javascript function, place it in application.js, and put the binding code in the view?
AKWF
bind by id: no, you don't have to. Maybe you've got two submit links on that form, so they wouldn't have unique ids and you'd bind by some class or other selector. If you do OJS, there would only be a link in the view, all the functionality would be in your application.js file (select link, bind to it, grab variables, submit form remotely, grab reply, update page [or not].) I'll add another answer with a code sample.
wesgarrison
+1  A: 

link_to_remote in the earlier versions of rails would have solved your problem. But from rails3 link_to_remote is no longer present as they moved away from obtrusive javascript. You could however use a link_to_function which can call a javascript function which will do the get for you. Refer http://stackoverflow.com/questions/2458841/link-to-syntax-with-rails3-link-to-remote-and-basic-javascript-not-working-in-a this discussion for more details.

Kunday
Thanks Kunday. Is calling a Javascript function in that way considered obtrusive?
AKWF
Nope, calling javascript function by that way would not be obtrusive. However there seems to be a shift in favour to evented JS programming by adding classes to the div's or a tags and then attach events from prototype/jQuery. Its better that way as dom is clean.
Kunday
Do you have any idea where I can find more information on that? I'm trying to adopt the the latest practices. Thanks!
AKWF
I'm not sure if there are documented pages around but can give you a sample code from my project though. `var Form = Class.create({ initialize: function (form) { this.cancelLink = this.form.invoke("select", "a.cancelLink").first(); this.hide(); }, show: function () { this.form.invoke("show"); }, hide: function () { this.form.invoke("hide"); }, bindEvents: function () { this.cancelLink.invoke("observe","click", this.hide.bindAsEventListener(this)); }});`
Kunday
PS, the comments dont seem to have got formatting of code, what that code basically does is finds any form on page and tries to attach show, cancel. so any form you drop in the project with a show and cancel button will get this behavior without doing any onclick=function() kind of approach
Kunday
+1  A: 

Ok, unobtrusive javascript time, based on the comments for my other answer.

I grabbed this from one of my projects, so you may need to tailor it to your needs, but it should give you an idea of what to do. jQuery 1.3.2, FYI.

  $('form.link_id').livequery('submit', function(){
    $.ajax({
      url: $(this).attr('action'),
      data: $(this).serializeArray(),
      type: 'POST',
      error: function(xhr, status, error){
        alert("Save failed. Please check the form and try again.\n" + (error || status));
      },
      success: function(content){
        // do something with reply if you want.
      },
      dataType: 'html'
    });
    return false;
  });

Then, the link is pretty basic:

<a href='/resource/new' id='link_id'>
wesgarrison
Wes, this is *exactly* what I needed to have this penetrate my thick skull. You are the MAN. Thanks a lot bro, I get it now.
AKWF