0

我有一个允许用户发布微博的应用程序,当发布微博时,我使用 ajax 和 jquery 更新页面上的帖子而不刷新页面。

直到几天前,这一切正常,但突然之间它随机运行,有时它不起作用,有时它会。发布了微博并更新了数据库,但我渲染的 js 模板似乎大部分时间都没有被触发。

Microposts 控制器创建动作:

def create
     @micropost = current_user.microposts.build(params[:micropost])

        respond_to do |format|
            if @micropost.save
            format.js   { render :post_on_wall }
            end
        end

end

Post_on_wall js 模板:

$('form#new_micropost').off().on('ajax:success', function(e){
  alert('working');

    e.preventDefault();

    var micropostsContainer = $(this).parent('div.microposts');


    micropostsContainer.find('div.postsContainer').prepend('<%= j render("users/partials/micropost") %>');
    micropostsContainer.find('div.postHolder:first').hide().slideDown(250);
    micropostsContainer.find('textarea#micropostBox')
        .removeClass("micropost_content_expanded")
        .addClass("micropost_content")
        .val("");
    micropostsContainer.find('div#micropostOptions').addClass('micropostExtraOptions');
    micropostsContainer.find('div#postOptions').hide();
    micropostsContainer.find('div.imagePreview').css({
                                                  marginBottom:'0px',
                                                  marginTop:'0px'
                                                  })
                            .children().remove();

});

这就是奇怪的地方。如果我删除 js 模板中的所有代码,除了警报之外,它就会被触发。放回所有代码后,警报不会被触发。现在,如果我删除呈现 micropost 部分的 erb 代码并在字符串中添加一些文本,则会触发 js 模板警报并使用文本更新页面。

我不知道为什么会这样。我什至从几天前回到了我的应用程序的一个版本,我遇到了同样的问题。我唯一改变的是rails版本。我从 3.2.0 升级到 3.2.3。

考虑到微帖子的任何方式仍在发布到数据库,知道为什么我会遇到这个 JS Ajax 问题吗?我实际上已经接近取消在我的表单中使用 remote_to 而是直接使用 JQuery $.ajax ,但我不应该这样做。我喜欢利用 rails 提供的功能。

这是我的堆栈跟踪:

Started POST "/microposts" for 127.0.0.1 at 2012-05-21 11:41:31 +0100
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1] Processing by MicropostsController#create as JS
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"4KVkocVy4ONTrhQ7DqBWsJXcZC8uYDOjmv6C1DlcRxg=", "micropost"=>{"photo_attributes"=>{"photo_album_id"=>"25"}, "user_id"=>"2", "content"=>"lkkllk", "link"=>""}, "commit"=>"Post"}
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]    (0.1ms)  BEGIN
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   SQL (0.3ms)  INSERT INTO `microposts` (`content`, `created_at`, `image`, `link`, `poster_id`, `updated_at`, `user_id`) VALUES ('lkkllk', '2012-05-21 10:41:31', NULL, '', NULL, '2012-05-21 10:41:31', 2)
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]    (0.3ms)  COMMIT
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   Micropost Load (0.7ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   Profile Load (0.3ms)  SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 2 LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   Photo Load (0.2ms)  SELECT `photos`.* FROM `photos` WHERE `photos`.`id` = 3 LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   Photo Load (0.3ms)  SELECT `photos`.* FROM `photos` WHERE `photos`.`micropost_id` = 4892 LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]    (0.2ms)  SELECT COUNT(*) FROM `comments` WHERE `comments`.`micropost_id` = 4892
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT COUNT(*) FROM `comments` WHERE `comments`.`micropost_id` = 4892
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   Photo Load (1.9ms)  SELECT `photos`.* FROM `photos` WHERE `photos`.`id` = 3 LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   CACHE (0.0ms)  SELECT `microposts`.* FROM `microposts` WHERE `microposts`.`user_id` = 2 ORDER BY microposts.created_at DESC LIMIT 1
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   Rendered users/partials/_micropost.html.erb (19.3ms)
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1]   Rendered microposts/post_on_wall.js.erb (20.8ms)
[afb16a524c6d01fd7408e9944dbf30f9] [127.0.0.1] Completed 200 OK in 33ms (Views: 20.0ms | ActiveRecord: 4.5ms | Sphinx: 0.0ms)

我真的很感激对可能发生的事情有所了解,以便我可以修复它。

亲切的问候

4

1 回答 1

0

Rails 不知道要渲染哪个实例变量。要么在 :locals 散列中传递 @micropost,要么只是“渲染 @micropost”。

请参阅这个非常有用的 railscast 以获得出色的模型:http ://railscasts.com/episodes/136-jquery

于 2012-05-21T20:41:03.270 回答