How can I output helper from erb.js in rails 3?

This is what I have in the erb.js file .... but this does not work:

  1 //Update with a message
  2 $('#follow-update').html("<%= follow_button(@question) %>")

follow_button - assistant:

20   def follow_button(resource)
 21     
 22     type = resource.class.name
 23     follow_status = current_user.following?(resource)
 24 
 25     verb = "Follow" if follow_status == false
 26     verb = "Unfollow" if follow_status == true
 27 
 28     content_tag(:div, :class => "follow-button") do
 29       link_to "#{verb} this #{type} ", follow_path(:followed_type => type,
 30                                                   :followed_id => resource.id,
 31                                                   :follow_verb => verb), 
 32                                                   :class => "button",
 33                                                   :remote => true
 34     end
 35   end
 36 end

The auxiliary stand-alone works fine, but I want it to update my button. Perhaps there is a way to simply update the text without replacing the entire helper?

+5
source share
4 answers

Create a party that contains your only your assistant.

For example: '/wherever/_follow_button.html.erb'

Inside, call your assistant:

<%= follow_button(question) %>

Then render using JS.

$('#some-id').html("<%= escape_javascript(render('/wherever/follow_button', :question => @question)) %>");

There are ways to make this more efficient, but hopefully it helps for now.

+4
source

You will need escape_javascript

$('#follow-update').html("<%= raw( escape_javascript( follow_button(@question ))) %>");

, . ajax html

0

You cannot call a helper method from a file js.erbunless you explicitly include it, as described in Using the Rails Helpers Method in a javascript resource . For instance:

<% environment.context_class.instance_eval { include ApplicationHelper } %>

And you have to be careful to elude your HTML.

0
source

Sorry, I can’t check right now, but have you noticed html safe or javascript screens?

eg. try to wrap follow_button(@question)in raw()or escape_javascript()?

-1
source

All Articles