gpt4 book ai didi

ruby-on-rails - 在 Posts 模型中保存的用户 ID,如何呈现该用户?

转载 作者:数据小太阳 更新时间:2023-10-29 07:51:02 25 4
gpt4 key购买 nike

当用户在个人资料上发帖时,个人资料所有者的用户 ID 将作为整数保存在 :poster(在 Post 模型中)中。我如何找到并呈现该用户/用户的信息?

更多信息:

  1. 使用Slugged gem
  2. 尝试生成帖子提要时,当我在 View 中调用 feed_item.user 时,它会将我定向到发布帖子的用户,而不是发布帖子的用户。

Feed 项目 View

enter image description here

提要 View

enter image description here

页面 Controller 主页函数

enter image description here

@postee 是我根据为每个帖子保存的用户 ID 查找用户的微弱尝试。理想情况下,我希望能够链接到发布帖子的用户,并在我的 View 中显示有关该用户的信息(例如用户的用户名)。我已经修修补补了一段时间但被卡住了,有什么想法吗?非常感谢您的帮助,如果需要任何其他信息,请告诉我!

编辑

发布表单 View

<%= form_for @post do |f| %>
<%= f.hidden_field :name, :value => current_user.name %>
<%= f.hidden_field :poster, :value => @user.id %>
<div class="postbox">

<div class="postfield">
<%= f.text_area :content %>
</div>
<div class="postsubmit">
<%= f.submit "Submit" %>
</div>
</div>

用户 Controller 显示 Action

def show
@user = User.find_by_cached_slug(params[:id])
@posts = Post.find_all_by_poster(@user.id)
@post = Post.new
if user_signed_in?
@post = Post.new
end

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end

最佳答案

您的方法有点奇怪,因为您使用属性 :poster 而不是 Rails 约定的 :user_id

无论如何,尝试在你的 View 中粘贴这样的东西

@post.poster.email # to return the email address of your user

上面的代码取决于你的用户模型有一个 :email 属性为您希望返回的任何属性更改电子邮件,无论是用户名等

您的模型中指定的帖子和用户之间是否存在关系?即 belongs_to :user

您有没有为相关记录使用传统的 :model_name_id 的原因吗?

关于ruby-on-rails - 在 Posts 模型中保存的用户 ID,如何呈现该用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6085414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com