Passing argument to '.js.erb' template

Nitish Upreti picture Nitish Upreti · Jun 19, 2012 · Viewed 14.1k times · Source

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?

Answer

dimuch picture dimuch · Jun 19, 2012

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.)