Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing argument to '.js.erb' template

I want to pass some arguments to the my Javascript template in Rails3 application What I try with respond_to block is:

 respond_to do |format|
      format.js({:id=>params[:id]})
    end

I also tried:

 respond_to do |format|
      format.js(params[:id])
    end

Am I forced to make id as an instance variable for the js template to use? How to pass variables to the template here?

like image 589
Nitish Upreti Avatar asked Jun 19 '12 17:06

Nitish Upreti


1 Answers

Does it work?

respond_to do |format|
  format.js { render "action", :locals => {:id => params[:id]} }
end

"action" is your action / template name (index, show, etc.)

like image 111
dimuch Avatar answered Sep 21 '22 01:09

dimuch