2

Create

if @image.save
    format.html { redirect_to vote_path(@image) }
    format.js { render :js => "my_function();" }
end

so I'd like to fire up a function after the redirect has happened, but this doesn't work. Are there any other methods I could try and could you suggest me any please?

Thank you.

3
  • What is the javascript supposed to do? Depending on the desired result, the answer can be different. For example if you just want to display a message, Rails has the built-in flash mechanism. Commented Aug 26, 2014 at 14:27
  • @p11y the funciton should $("#share_window").show(); Commented Aug 26, 2014 at 14:30
  • You can just put it in the view. Commented Aug 26, 2014 at 14:35

2 Answers 2

1

The redirect and render are separate. If you want it to run JS after the redirect, you can put some in the view that the vote_path is using. If you only want it to run after the redirect and not on the page load, you can set a flash before you redirect, and then check for the flash in the view.

Sign up to request clarification or add additional context in comments.

Comments

0

An alternative would be to create a view to create.js.erb, and it run JS:

Your controller:

if @image.save
  format.html { redirect_to vote_path(@image) }
  format.js { }
end

create.js.erb

$("#share_window").show();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.