在控制器中创建了一个新的Active Record模型,但除非我刷新,否则它不会显示在视图上(Created a new Active Record model in controller but it doesn't show up on view unless I refresh)
在控制器中,特别是编辑操作,我以与此相同的方式创建了一个新的Active Record模型:
@newPost = Post.new @newPost.update_attribute(:title, @some_title) @newPost.update_attribute(:completed, 0) @newPost.save @all_user_posts = @user.posts现在,当我尝试调用@all_user_posts并在视图中显示它时,除非我刷新页面,否则它不会显示。 这里发生了什么,是否有更好的方法来解决这个问题,以便我可以在创建新帖子时立即显示新帖子而无需刷新页面?
In the controller, specifically the edit action, I created a new Active Record model in the same manner as this:
@newPost = Post.new @newPost.update_attribute(:title, @some_title) @newPost.update_attribute(:completed, 0) @newPost.save @all_user_posts = @user.postsNow, when I try to call on @all_user_posts and show it in the view, it doesn't show up unless I refresh the page. What's going on here and is there a better way to approach this so that I can show the new posts as soon as I create them without having to refresh the page?
最满意答案
我想我其实可以这样做:
@all_user_posts = Post.where("user_id =?", @user.id)但是,有人还能解释为什么会这样吗?
I figured I can actually just do this:
@all_user_posts = Post.where("user_id =?", @user.id)But, could someone still explain why it happens?
更多推荐
发布评论