2017-05-12 21 views
6

Tôi có một biểu mẫu Devise được thiết lập trên ứng dụng của mình để cập nhật một phần thông tin của người dùng (trường địa chỉ giao hàng) từ một trang khác (charges#new) và đầu ra của máy chủ dường như cho biết nó đang hoạt động :AJAX Devise Form with Rails không cập nhật dữ liệu

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) 

Tuy nhiên, khi tôi kiểm tra bảng điều khiển, nó vẫn chưa cập nhật thông tin.

registrations_controller của tôi là:

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 

Tôi đã sau update.js.erb:

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

Đây là tôi 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 --> 

Và đây là một phần _shipping.html.erb:

<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 --> 

Có ai có thể xem tại sao điều này không cập nhật bảng không?

EDIT: ĐƯỜNG Theo yêu cầu, đây là tuyến đường của tôi:

  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 

Và đây là tuyến đường có liên quan của tôi:

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 
+0

Chia sẻ tuyến đường của bạn..cũng chạy 'rake tuyến' và thêm kết quả .. –

+0

@ Md.FarhanMemon, theo yêu cầu, tôi đã thêm đầu ra tuyến đường rake. – Liz

+0

Bạn đang sử dụng phiên bản Rails và Devise nào? –

Trả lời

3

tôi đã kết thúc việc đưa một phương pháp update trong tôi users_controller (trái ngược với số registrations_controller) của tôi:

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 

Thêm tuyến đường thích hợp, và thay đổi dòng simple_form_for tới:

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

Và đó đã làm nó.

Các vấn đề liên quan