javascript - Rails Redirect Not Going as HTML - Stack Overflow

admin2025-04-20  0

I have a sign up form that should redirect the user to their profile if sign up is successful. If registration fails I want to put up an error message without reloading the page using Javascript.

Here's my form header:

<%= form_for resource, as: resource_name, url: registration_path(resource_name), remote: true do |f| %>

My registration controller:

def create
  respond_to do |format|
    if resource.saved
      sign_up(resource_name, resource)
      format.html { redirect_to current_employer }
    else
      format.js { render 'employers/sign_up_failure' }
    end
  end
end

So the failure works. It displays an error message above the form without reloading the page. The successful registration format.html { redirect_to current_employer } is not working.

My server log says the redirect was handled:

Redirected to http://localhost:3000/employers/27

But it looks like the show action on my Employers controller is being called as Javascript:

Processing by EmployersController#show as JS

So when I submit the form, it does not redirect me to my profile page. The page doesn't flicker. I am logged in though, so if I refresh the page it shows that I am logged in.

BTW, I am using Devise and my Registration controller is an amendment to what Devise uses.

Not sure why this isn't working. I've taken out the format.js {} part and it still sends the GET call to my Employers#Show action via JS. What I'd like is for the redirect to be formatted as HTML and take me to the profile page.

Any ideas?

SOLUTION Thank you for you help. Face palm, I totally forgot about how remote: true works. Here is my implemented solution... hopefully someone finds this useful.

def create
  respond_to do |format|
    if resource.saved
      sign_up(resource_name, resource)
      #CHANGED
      format.js { render js: "window.location.href = '/#{resource_name}s/#{resource.id}'" }
    else
      format.js { render "#{resource_name}s/sign_up_failure" }
    end
  end
end

This also improved the reusability of the code.

I have a sign up form that should redirect the user to their profile if sign up is successful. If registration fails I want to put up an error message without reloading the page using Javascript.

Here's my form header:

<%= form_for resource, as: resource_name, url: registration_path(resource_name), remote: true do |f| %>

My registration controller:

def create
  respond_to do |format|
    if resource.saved
      sign_up(resource_name, resource)
      format.html { redirect_to current_employer }
    else
      format.js { render 'employers/sign_up_failure' }
    end
  end
end

So the failure works. It displays an error message above the form without reloading the page. The successful registration format.html { redirect_to current_employer } is not working.

My server log says the redirect was handled:

Redirected to http://localhost:3000/employers/27

But it looks like the show action on my Employers controller is being called as Javascript:

Processing by EmployersController#show as JS

So when I submit the form, it does not redirect me to my profile page. The page doesn't flicker. I am logged in though, so if I refresh the page it shows that I am logged in.

BTW, I am using Devise and my Registration controller is an amendment to what Devise uses.

Not sure why this isn't working. I've taken out the format.js {} part and it still sends the GET call to my Employers#Show action via JS. What I'd like is for the redirect to be formatted as HTML and take me to the profile page.

Any ideas?

SOLUTION Thank you for you help. Face palm, I totally forgot about how remote: true works. Here is my implemented solution... hopefully someone finds this useful.

def create
  respond_to do |format|
    if resource.saved
      sign_up(resource_name, resource)
      #CHANGED
      format.js { render js: "window.location.href = '/#{resource_name}s/#{resource.id}'" }
    else
      format.js { render "#{resource_name}s/sign_up_failure" }
    end
  end
end

This also improved the reusability of the code.

Share Improve this question edited Jan 3, 2017 at 8:18 sinsuren 1,7542 gold badges24 silver badges26 bronze badges asked Jun 4, 2014 at 13:29 HunterHunter 1,68717 silver badges24 bronze badges 1
  • 2 You have remote: true on your form. Your form will always be submitted by Ajax rather than by the browser's normal submit mechanism and therefore format.html will never work. You will have to handle redirection via javascript – Joseph N. Commented Jun 4, 2014 at 13:41
Add a ment  | 

1 Answer 1

Reset to default 7

Because you have remote: true on the form it is submitted via ajax, you haven't said what to do if submitted via ajax and successful. The format requested is js not html which is why the redirect isn't working.

You'll need to create a create.js.erb file to serve if successful, handling the redirect through javascript with window.location = "wherever you want to go"

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745123036a286261.html

最新回复(0)