cvdv
cvdv

Reputation: 2932

format.js not executing (ajax)

I'm trying to add email confirmation when signing up users. Users have to fill out a form with their invitation code and email address, and then they get an email with an activation link.

So all I want to do is have it so when the user fills out the form, it submits the form and reloads the form so that it hides and displays the message "we sent you an email, please check and click the link" instead of reloading the entire page.

signup.html.erb

here I've added remote: true

<%= form_tag(form_action, method: :get, remote: true, id: 'create-form') do %>

users_controller.rb

the redirect works but format.js is an unknown format

def new 

... code checking params

if @user.user_email_confirmed
  render :new
else
  respond_to do |format|
    format.html { redirect_to signup_path(@user.name, email: @user.email) }
    format.js
  end
end

new.js.erb

$('#create-form').hide().after('');
$('.login-form').append('<p class="link-sent">We have sent an activation link to the email address you entered - please be sure to check your spam or trash folders. Please click that link to complete account activation.</p>');

not sure what I am missing, any ideas?

Upvotes: 1

Views: 143

Answers (1)

svelandiag
svelandiag

Reputation: 4320

I really do not think that the action the form is calling when submitting is your new action, it may be some create action in your controller.

So when you hit the "submit" button of your form it is sent through Ajax due to you have remote: true option, and the action it is calling does not have a format.js nor js.erb view, That's why you get unknown format.

Make sure which controller action is being called when you submit your form, then add the format.js and the proper js.erb view.

Please take a look to this blog post, it may be helpful.

Upvotes: 1

Related Questions