ruby-on-rails - 带有 Rails 的 AJAX 设计表单不更新数据

标签 ruby-on-rails ajax devise

我在我的应用程序上设置了一个设计表单,用于更新来自不同页面 ( charges#new ) 的用户信息的一部分(送货地址字段),服务器输出似乎表明它正在工作:

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)

但是,当我检查控制台时,它仍然没有更新信息。

我的 registrations_controller这是:
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

我有以下 update.js.erb :
$(".shipping-info").html("<%= escape_javascript(render 'charges/shipping') %>")

这是我的 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 -->

这是_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 -->

谁能看出为什么这不更新表格?

编辑:路线
根据要求,这是我的路线:
           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

这是我的相关路线:
  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

最佳答案

我最后放了一个 update我的 users_controller 中的方法(与我的 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

添加适当的路由,并更改 simple_form_for线到:
d<%= simple_form_for(@user, url: user_path(@user), html: { method: :put }, remote: true) do |f| %>

做到了。

关于ruby-on-rails - 带有 Rails 的 AJAX 设计表单不更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43927571/

相关文章:

ruby-on-rails - 从父 Factory Girl 工厂访问子属性

ruby-on-rails - 在验收规范中测试或不测试什么?

jquery - elasticsearch.js 客户端连接被拒绝 : Access-Control-Allow-Origin not recognized?

javascript - MVC 中的 Ajax POST

ruby-on-rails - 设计:在注册过程中禁用密码确认

ruby-on-rails - 由于无效的真实性 token ,无法使用设备在 rails 中注销

ruby-on-rails - Rails 设计 : user_signed_in? 不工作

ruby-on-rails - 管理 Rails 的 gem 版本/依赖项

ruby-on-rails - Rails 4 错误,每个命令 "` load' : no implicit conversion of nil into String"(Mac OS X 10. 9)

php - alt-tab 类型的网页设计