简体   繁体   中英

update.js in Rails needs second submit

I am having a small (really small) issue with Rails.

A remote:true update form (calling update.js.erb) needs two submissions instead of one to correctly refresh the view.

def update
@user = User.find(params[:id])
if @user.update_attributes(update_user_params)
  respond_to do |format|
    format.html {
      flash[:success] = "Votre profil a été mis à jour."
      redirect_to @user
    }
    format.js
  end
else
  render 'edit'
end

end

And here is the view code (I am using method: 'put' because I am calling it from the homepage, and not the edit.html, but even without it, the problem persists):

<%= form_for @user, html: { multipart: true }, remote: true, method: 'put' do |f| %>
  <%= f.label :location, 'Ville', class: 'popover-title' %>
  <%= f.select :location, cities_options %>
  <%= f.submit "Valider", class: 'btn-addon' %>
<% end %>

Here's what I see on submit:

Started PUT "/users/101" for 127.0.0.1 at 2015-08-19 15:25:05 +0200
Processing by UsersController#update as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hZMd5usgyZVgY4DxqNp0PxF8JPvRk/V+WAGTjFLhj8rqS0y1UCbzbQ9e7HYH20oNdn3TK6FXrLgeDXYnrZeVIg==", "user"=>{"location"=>"Paris"}, "commit"=>"Valider", "id"=>"101"}
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 101]]
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 101]]
CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "101"]]
(0.0ms)  begin transaction
User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('andrey.soloviev.s@gmail.com') AND "users"."id" != 101) LIMIT 1
SQL (0.3ms)  UPDATE "users" SET "location" = ?, "lat" = ?, "lng" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["location", "Paris"], ["lat", 48.856614], ["lng", 2.3522219], ["updated_at", "2015-08-19 13:25:05.511789"], ["id", 101]]
(2.2ms)  commit transaction
Rendered users/update.js.erb (0.1ms)
Completed 200 OK in 208ms (Views: 4.6ms | ActiveRecord: 2.9ms)

And finally, here is the update.js.erb:

$('.location-name').html('<%= current_user.location %>');

Basic Rails js, nothing out of the ordinary. The above code tells me that everything appears to be working properly. However, on submit, update.js refreshes the location with the old value from the database (that was there before the new one). Only on second submit I get to see the correct value appearing.

If anyone has any ideas or solutions, I am forever grateful.

Thank you all in advance!

I assume current_user is a helper method on the controller, which I guess is cached. Switch it to the user object you updated. $('.location-name').html('<%= @user.location %>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM