Rails: accessing a non-instance variable in js.erb?

This is one of the classic issues of RJS templates.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a page that renders multiple forms. Currently, when the user submits any one of these forms, it updates (via ajax) a div on the same page with the content from the form that was just submitted. I also want to remove() the form element that was just submitted after the ajax post request is completed.

However, I need to be able to access that specific form ID within the js. Erb file to do so. Since my page has x number of forms rendered dynamically, I cannot simply access an instance variable in my js.erb.

Page: 'form', :locals => { :peer_review => peer_review } %> The @peer_reviews instance variable contains an array of new PeerReview objects already containing some data. Form: "> { :method => "post" }, :remote => true do |f| %> Peer Review for: : 'None' } %> peer_review. User_id %> peer_review.

Reviewee_id %> peer_review. Review_period_id %> js. Erb: $("#completed_peer_reviews").

Html(""); I was hoping to just add another line to the js. Erb file that removes the form element that just triggered the execution of the js. Erb file like so: $("#peer_review_form_").remove(); How should I actually be referencing peer_review.

Reviewee_id here? Or should I be taking a completely different approach? Javascript jquery ajax ruby-on-rails-3 forms link|improve this question asked Dec 3 '11 at 20:05anxiety1358 93% accept rate.

This is one of the classic issues of RJS templates. Quick answer: If you simply want to solve the problem, you could pass along some temporary id to identify the form. E.

G: # in the index 'form', :locals => { :peer_review => peer_review, :i => I } %> # then in the form (note, you don't need to specify POST in a form_for) "> true do |f| %> # finally in the create js. Erb $("#peer_review_form_").remove(); Longer Answer: That being said, while RJS templates were "the Rails way" for a long time, they've since fallen out of favor. The more modern method is typically client side JS templates with a JSON API, rather than running server generated JS templates (RJS).

This has a lot of advantages, one being that the DOM binding issue you're having right now no longer exists. This is an example of how you might do this with pure jQuery, but there are many templating options out there. Then you'd create a handler and bind it to a successful ajax response.

This would require that your peer_reviews#create action responded to JSON: $('form. New_peer_review'). Bind("ajax:success", function(data) { // remove the form which triggered the creation $(this).remove(); // then render the data into a template, and append it to the list $("#peer_review_tmpl").

Tmpl(data). AppendTo("#completed_peer_reviews"); }).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions