在帖子模型中保存的用户 ID,如何呈现该用户?
当用户在个人资料上发表帖子时,个人资料所有者的用户 ID 将以整数形式保存在 :poster 中(在 Post 模型中)。如何查找并呈现该用户/用户的信息?
更多信息:
- 使用 Slugged gem
- 尝试生成帖子提要,当我在我看来,调用 feed_item.user ,它会将我定向到发布该帖子的用户,但不会定向到发布该帖子的个人资料的用户。
Feed 项目视图
Feed 视图
页面控制器主页功能
@postee 是我无力的尝试根据查找用户为每个帖子保存的用户 ID。理想情况下,我希望能够链接到发布帖子的个人资料的用户,并在我的视图中显示有关该用户的信息(例如用户的用户名)。我已经修补了一段时间并且被困住了,有什么想法吗?非常感谢您的帮助,如果需要任何其他信息,请告诉我!
编辑
发布表单视图
<%= 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>
用户控制器显示操作
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
When a user makes a post on a profile, the profile owner's user ID is saved as an integer in :poster (in the Post model). How do I find and render that user/user's information?
More information:
- Using Slugged gem
- Trying to generate a feed of posts, when I call feed_item.user in my view, it directs me to the user that posted the post, but not to the user who's profile the post was made on.
Feed Item View
Feed View
Pages Controller Home Function
@postee is my feeble attempt to find the user based on the user id saved for each post. Ideally I would like to be able to link to the user who's profile the post was made on and also display information about that user in my view (e.g. the user's username). I've been tinkering for a while and am stuck, any thoughts? Thank you very much for your help and please let me know if any additional information is needed!
EDIT
Post Form 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>
User Controller Show 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的方法有点奇怪,因为你使用属性
:poster
而不是:user_id
这是rails约定无论如何,尝试在你的视图中粘贴类似的东西
上面的代码取决于您的用户模型是否具有
:email
属性更改您希望返回的任何属性的电子邮件,无论是用户名等
您的模型中指定的帖子和用户之间是否存在关系?即
belongs_to :user
您是否有理由不使用传统的 :model_name_id 作为关联记录?
your method is slightly odd, in that you use the attribute
:poster
instead of:user_id
which is the rails conventionAnyway, try sticking something like this in your view
The code above depends on your user model having an
:email
attributechange email for whatever attribute you wish to return, whether it be username etc
Do you have a relationship between posts and users specified in your model? i.e.
belongs_to :user
IS there a reason why you are not using the conventional of :model_name_id for your associated record?
最终,我决定以非轨道方式进行,只是为了让功能正常工作。虽然我怀疑如果你能以某种方式将 :poster 中的用户 ID 绑定到一个新模型,并设置(有很多,属于,等等......)关系,那么这样会工作得更好。 (我相信这就是stephenmurdoch的答案所尝试的,只是存在一个问题:user_id已经与发帖用户绑定在一起。)
我在每个帖子发布时保存了更多的用户信息,然后为每个帖子渲染了个人资料帖子接收者的信息饲料项目。另外,因为用户具有基于cached_slug的唯一URL,所以我能够使用<%= link_to feed_item.postername, feed_item.postercachedslug %>创建指向发布帖子的个人资料的链接。
发布表单视图
提要项目视图
Ultimately, I decided to do it in a non-railsy way, just to get the functionality working. Though I suspect that if you can somehow tie the user id in :poster to a new model with (has many, belongs to, etc...) relationships set up, it would work much better that way. (I believe that this is what stephenmurdoch's answer was attempting, there was just a problem with :user_id already being tied to the posting user.)
I saved more user information when each post was made, and then rendered the profile post receiver's information for each feed item. Also because user's have unique URLs based on cached_slug, I was able to use <%= link_to feed_item.postername, feed_item.postercachedslug %> to create a link to the profile the post was made on.
Post Form View
Feed Item view