How to render a js.erb with the same name as a member action in Rails?

455 views Asked by At

I'd think this would go automatically, as I have already managed to do so for my destroy action, but no JS in activated.

My controller action looks like this:

  def visability_toggle
    @picture = Picture.find(params[:id])
    @picture.toggle! :visable

    respond_to do |format|
      format.js { render :action => 'visability_toggle' }
    end
  end

And for now my pictures/_visability_toggle.js.erb that I would like to match my controller looks like this:

alert('Picture id: #{@picture.id}');

The action itself works. Also this JS is correct and worked when I added it with render js: ... in the controller action.

2

There are 2 answers

0
Code-MonKy On

As mentioned: I should have left the underscore out when naming my template.

0
Karthick Nagarajan On

Just try this..

respond_to do |format|
    format.js { :location => path_to_controller_method_url(argument) }
end

How do you respond_to another js file in the controller using Ruby on Rails?

respond_to do |format|
    format.js {
       :template => "pictures/visability_toggle.js.erb", 
       :layout => false
    }
end