在控制器中创建了一个新的Active Record模型,但除非我刷新,否则它不会显示在视图上(Created a new Active Record model in controller but i

编程入门 行业动态 更新时间:2024-10-27 00:27:32
在控制器中创建了一个新的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.posts

Now, 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?

更多推荐

本文发布于:2023-08-03 16:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   非我   模型   器中   Record

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!