Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX Devise Form with Rails not Updating Data

I have a Devise form set up on my app to update a portion of the user's information (shipping address fields) from a different page (charges#new) and the server output seems to indicate it's working:

Started PUT "/users" for ::1 at 2017-05-11 17:10:52 -0700
Processing by RegistrationsController#update as JS
  Parameters: {"utf8"=>"✓", "user"=>{"street_address_1"=>"**street address**", "street_address_2"=>"", "city"=>"**city**", "state"=>"CA", "zip"=>"**zip code**", "provence"=>"", "country"=>"United States", "has_shipping"=>"true"}, "commit"=>"Calculate Shipping"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Order Load (0.1ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1  [["id", 5]]
  Rendered charges/_shipping.html.erb (5.0ms)
  Rendered devise/registrations/update.js.erb (6.4ms)
Completed 200 OK in 150ms (Views: 26.1ms | ActiveRecord: 0.3ms)

However, when I check the console it still hasn't updated the information.

My registrations_controller is this:

class RegistrationsController < Devise::RegistrationsController
  respond_to :html, :js

  private

  def sign_up_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :phone, :stripe_customer_id, :street_address_1, :street_address_2, :city, :state, :zip, :provence, :country, :has_shipping)
  end

  def account_update_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :phone, :admin, :stripe_customer_id, :street_address_1, :street_address_2, :city, :state, :zip, :provence, :country, :has_shipping)
  end
end

I have the following update.js.erb:

$(".shipping-info").html("<%= escape_javascript(render 'charges/shipping') %>")

This is my charges#new:

<div class="product-row black-border-row row" style="margin-bottom: 60px">
  <div class="container">
    <%= render "shipping" %>
  </div> <!-- page container -->
</div> <!-- product row -->

And here's the _shipping.html.erb partial:

<div class="row text-center">
  <h3>Shipping Address</h3>
  <%= simple_form_for(@user, url: registration_path(@user), html: { method: :put }, remote: true) do |f| %>
    <div class="form-inputs text-left">
      <div class="form-group col-sm-6">
        <%= f.label :street_address_1 %>
        <%= f.text_field :street_address_1, class: "form-control" %>
      </div>
      <div class="form-group col-sm-6">
        <%= f.label :street_address_2 %>
        <%= f.text_field :street_address_2, class: "form-control" %>
      </div>
      <div class="form-group col-sm-6">
        <%= f.label :city %>
        <%= f.text_field :city, class: "form-control" %>
      </div><div class="form-group col-sm-3 col-xs-6">
        <%= f.label :state %>
        <%= f.text_field :state, class: "form-control" %>
      </div><div class="form-group col-sm-3 col-xs-6">
        <%= f.label :zip %>
        <%= f.text_field :zip, class: "form-control" %>
      </div><div class="form-group col-sm-6">
        <%= f.label :provence %>
        <%= f.text_field :provence, class: "form-control" %>
      </div><div class="form-group col-sm-6">
        <%= f.label :country %>
        <%= f.text_field :country, class: "form-control" %>
      </div><div class="form-group">
        <%= f.hidden_field :has_shipping, value: true %>
      </div>
    </div> <!-- form inputs -->
      <%= f.button :submit, "Calculate Shipping" %>
  <% end %>
</div> <!-- shipping row -->

Can anyone see why this isn't updating the table?

EDIT: ROUTES As requested, here are my routes:

           orders_update GET    /orders/update(.:format)          orders#update
        new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
            user_session POST   /users/sign_in(.:format)          devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
           user_password POST   /users/password(.:format)         devise/passwords#create
       new_user_password GET    /users/password/new(.:format)     devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                         PATCH  /users/password(.:format)         devise/passwords#update
                         PUT    /users/password(.:format)         devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)           registrations#cancel
       user_registration POST   /users(.:format)                  registrations#create
   new_user_registration GET    /users/sign_up(.:format)          registrations#new
  edit_user_registration GET    /users/edit(.:format)             registrations#edit
                         PATCH  /users(.:format)                  registrations#update
                         PUT    /users(.:format)                  registrations#update
                         DELETE /users(.:format)                  registrations#destroy
                    user GET    /users/:id(.:format)              users#show
              home_index GET    /home/index(.:format)             home#index
                    root GET    /                                 home#index
               home_info GET    /home/info(.:format)              home#info
             home_export GET    /home/export(.:format)            home#export
               home_kits GET    /home/kits(.:format)              home#kits
                products GET    /products(.:format)               products#index
                         POST   /products(.:format)               products#create
             new_product GET    /products/new(.:format)           products#new
            edit_product GET    /products/:id/edit(.:format)      products#edit
                 product GET    /products/:id(.:format)           products#show
                         PATCH  /products/:id(.:format)           products#update
                         PUT    /products/:id(.:format)           products#update
                         DELETE /products/:id(.:format)           products#destroy
                     tag GET    /tags/:tag(.:format)              products#index
                    cart GET    /cart(.:format)                   carts#show
             order_items POST   /order_items(.:format)            order_items#create
              order_item PATCH  /order_items/:id(.:format)        order_items#update
                         PUT    /order_items/:id(.:format)        order_items#update
                         DELETE /order_items/:id(.:format)        order_items#destroy
                  orders POST   /orders(.:format)                 orders#create
              edit_order GET    /orders/:id/edit(.:format)        orders#edit
                   order GET    /orders/:id(.:format)             orders#show
                         PATCH  /orders/:id(.:format)             orders#update
                         PUT    /orders/:id(.:format)             orders#update
                 charges GET    /charges(.:format)                charges#index
                         POST   /charges(.:format)                charges#create
              new_charge GET    /charges/new(.:format)            charges#new
             edit_charge GET    /charges/:id/edit(.:format)       charges#edit
                  charge GET    /charges/:id(.:format)            charges#show
                         PATCH  /charges/:id(.:format)            charges#update
                         PUT    /charges/:id(.:format)            charges#update
                         DELETE /charges/:id(.:format)            charges#destroy

And here are my relevant routes:

  get 'orders/update'

  devise_for :users, :controllers => { registrations: 'registrations' }
  resources :users, only: [:show]

  get 'home/index'
  root 'home#index'

  get 'home/info'
  get 'home/export'
  get 'home/kits'

  resources :products
  get 'tags/:tag', to: 'products#index', as: :tag
  resource :cart, only: [:show]
  resources :order_items, only: [:create, :update, :destroy]
  resources :orders, only: [:update, :edit, :show, :create]

  resources :contacts
  put "contacts/:id/archive" => "contacts#archive", as: "archive_contact"
  put "contacts/:id/unarchive" => "contacts#unarchive", as: "unarchive_contact"

  resources :charges
like image 929
Liz Avatar asked May 12 '17 00:05

Liz


1 Answers

I ended up putting an update method in my users_controller (as opposed to my registrations_controller):

  def update
    @user = User.find(params[:id])
    @user.update(account_update_params)
  end

  def account_update_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :phone, :admin, :stripe_customer_id, :street_address_1, :street_address_2, :city, :state, :zip, :provence, :country, :has_shipping)
  end

Adding the appropriate route, and changing the simple_form_for line to:

d<%= simple_form_for(@user, url: user_path(@user), html: { method: :put }, remote: true) do |f| %>

And that did it.

like image 130
Liz Avatar answered Nov 19 '22 19:11

Liz