Saturday, September 10, 2011

[Rails] Re: Finding HTML attributes with jQuery in Rails 3.1

Frederick Cheung wrote in post #1021129:
> On Sep 10, 12:18pm, dwormuth <dworm...@post.harvard.edu> wrote:
>> "operations" )) %>").show()
>> $("#services").html("<%= escape_javascript(render(:partial => foo ))
>> %>").show()
>>
>> or
>>
>> $("#services").html("<%= escape_javascript(render(:partial => $
>> (this).data("section") )) %>").show()
>>
>
> This isn't possible. Whatever is in your <%= %> bits is evaluated when
> the response is served by your browser.

Wrong(a typo?). The way it works is:

1) The browser makes a request.
2) The server prepares the response, and evaluates the <% ... %> bits.
3) The server sends the response to the browser.
4) The browser takes action based on the response.

The browser certainly doesn't evaluate the <% ... %> bits.

> It can't use information that
> will only come into existence once the user has started to interact
> with the page in the browser. You'd either need to have all those
> partials hidden somewhere on the page so that the js can find them or
> make an Ajax request once you know which partial is needed.
>

The link does send an ajax request--that is what the attribute:

data-remote="true"

means.


The problem with this js:

$("#services").html("<%= escape_javascript(render(:partial =>
"operations" )) %>").show()

is that this html:

<a href="/toggle" data-remote="true" section="training">

sends a GET request to the server using ajax (data-remote="true"), but
as you can see, the url "/toggle' does not have a query string attached,
e.g.:

<a href="/toggle?section=training" data-remote="true"
section="training">

Without a query string, the server has no way of knowing which html
element sent the GET request. Although, somehow the server does know
that the request is from an html element that has an attribute
data-remote="true". Rails treats other ajax requests differently: a
respond_to block like this:

respond_to do |format|
format.js{render :partial => 'a_partial_name'}
format.html {}
end

will cause a data-remote="true" ajax request to hit the format.js block,
while an ajax request like this:


$("#services").load('action_name', data);

will hit the format.html block.


If you want to avoid having to add query strings to all the urls in the
<a> tags, you can use js to intercept the click on the link. js can
read the attributes of the html element that was clicked, and then js
can send an ajax request for the proper html page based on the 'section'
attribute. When the js gets the response from the server, it can insert
the returned html into the current page.


app/views/layouts/application.html.erb:

<script type='text/javascript' >

$(document).ready(function() {

$(".insert").click(function(event) {
var section = $(this).attr("section");
var data = "section=" + section;

$("#services").load('action_name', data);

event.preventDefault(); //prevents going to href in <a> tag
});


});

</script>


And in the controller:

def action_name

respond_to do |format|

format.html{
if request.xhr?
render :partial => params[:section]
end
}

end

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate