我有一个允许用户发布微博的应用程序,当发布微博时,我使用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模板中除了警报之外的所有代码,那么就会被解雇。将所有代码放回后,警报不会被触发。现在,如果我删除渲染微博部分的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)
我真的很感激对可能发生的事情有所了解所以我可以解决它。
亲切的问候
答案 0 :(得分:0)
Rails不知道要渲染的实例变量。要么在:locals哈希中传递@micropost,要么只是'渲染@micropost'。
对于一个出色的模型,请参阅这个非常有用的railscast:http://railscasts.com/episodes/136-jquery