使用.js.erb文件(远程真实触发器)在Rails中渲染部分不起作用

时间:2014-07-16 14:45:55

标签: ruby-on-rails partial

我试图在用户更改其个人资料头像时呈现部分(与页面重新加载相反)。问题是.js.erb文件似乎根本没有被触发(我的控制台日志消息不打印)。我似乎没有收到任何错误,因为它只是将页面重定向到根目录。

在我的服务器日志中(如下所示),存在令牌真实性错误,但即使我禁用身份验证并且错误消失,重定向仍然会发生,并且.js.erb基本上被忽略。

Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f9ae101c3f8 @tempfile=#<Tempfile:/tmp/RackMultipart20140716-9847-v0ray3>, @original_filename="creator.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"creator.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Upload Avatar", "id"=>"2"}
Can't verify CSRF token authenticity
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
   (0.0ms)  begin transaction
   (0.1ms)  commit transaction
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "2"]]
   (0.0ms)  begin transaction
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  SQL (0.1ms)  UPDATE "users" SET "avatar" = ?, "updated_at" = ? WHERE "users"."id" = 2  [["avatar", "creator.jpg"], ["updated_at", Wed, 16 Jul 2014 14:41:14 UTC +00:00]]
   (160.8ms)  commit transaction
Redirected to http://localhost:3000/

.js.erb文件

$(".avatar-container").html("<%= escape_javascript(render(:partial => 'profile/avatarContainer')) %>");

用户控制器

  def update
    @user = User.find(params[:id])
    respond_to do |format|
        if @user.update_attributes(params.require(:user).permit!)
          format.js
          format.html{
            redirect_to root_path
          }      
        else
          format.js
          format.html{redirect_to root_path}
        end
    end
  end

部分

-if current_user.avatar.url != "/assets/userProfile_dashboard.png"
  .avatar-large
    =image_tag current_user.avatar.url
    = simple_form_for(current_user, html: {:multipart => true }, remote: true) do |f|
      = f.file_field :avatar, style:"margin-top:-258px;", id: "profile-avatar"
      = f.button :submit, "Upload Avatar", :class => "buttonRoundedCornersGreen ", style: "width:130px;margin-top:20px;", id: "avatar-submit"
-else
  .no-avatar 
    = simple_form_for(current_user, html: {:multipart => true }, remote: true) do |f|
      = f.file_field :avatar, id: "profile-avatar" 

修改: 经过大量搜索后,似乎对文件上传和远程真有一个多部分为creating a conflict。我可以通过安装Remotipart gem来解决这个问题。

1 个答案:

答案 0 :(得分:2)

经过大量搜索后,似乎对文件上传和远程为true的多部分为true creating a conflict。我可以通过安装Remotipart gem来解决这个问题。