2

I have a profile edit form inside which I have button which toggles another form for changing password.

My routes look like this:

resource :profile do
  get '/password_edit' => 'profiles#password_edit', as: 'edit_password'
  patch '/password_update' => 'profiles#password_update', as: 'change_password'
end

Custom controller methods:

def password_edit
  render partial: 'edit_password'
end

def password_update
  if @user.authenticate(params[:user][:current_password]) &&  @user.update_attributes(user_params)
    redirect_to root_url
  else
    render :edit
  end
end

And the form:

<div class="password-edit">
<%= form_for @user, {url: '/change_password_profile'} do |f| %>
  <div class="password" style="display:none; height:320px;">
    <%= render partial: "password_form", locals: { f: f } %>
  <div>
    <%= f.submit "Update password", class: "btn btn-primary" %>
    <%= link_to "cancel and go back", admin_accounts_path, class: "btn btn-link" %>
  </div>
  </div>
<% end %>
</div>

The profiles.js:

$(function(){
  $(".expand_password_edit_form").on("click", function(event){
    $(".password-edit").find('.password').slideToggle(400,
    function(){
      $('html, body').animate({
        scrollTop: $('.password').offset().top + $('window').height()
    }, 1000);
    });
  });
});

However, when I click the submit button, it's being handled by the update action, not by the password_update one. I know it's because I'm rendering this form through js and the route does not change.

How can I accomplish this?

Bogdan Popa
  • 1,099
  • 1
  • 16
  • 37
  • 1
    What is the content of "password_form" partial? I think that you are rendering form inside another form, which is not valid (http://stackoverflow.com/a/555970/2549660) – Paulius May 19 '15 at 17:29
  • Yeah, I just found out that it's not valid html. I tried to do something which I thought was nice. Maybe I'll just toggle the password fields. – Bogdan Popa May 19 '15 at 17:37
  • I did all this because I didn't want to call authenticate(params[:current_password]) on the default update method. – Bogdan Popa May 19 '15 at 17:37
  • I guess validate on context would be the solution – Bogdan Popa May 20 '15 at 12:20

0 Answers0