Posts模型中保存的用户ID,如何呈现该用户?(Saved user id in Posts model, how do I render that user?)

系统教程 行业动态 更新时间:2024-06-14 17:02:17
Posts模型中保存的用户ID,如何呈现该用户?(Saved user id in Posts model, how do I render that user?)

当用户在配置文件上发布帖子时,配置文件所有者的用户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

最满意答案

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

无论如何,尝试在你的视图中坚持这样的东西

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

上面的代码取决于您的用户模型,其中包含:email属性更改电子邮件,用于您希望返回的任何属性,无论是用户名等

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

您是否有理由不使用传统的:model_name_id作为相关记录?

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

<%= form_for @post do |f| %> <%= f.hidden_field :name, :value => current_user.name %> <%= f.hidden_field :poster, :value => @user.id %> <%= f.hidden_field :postername, :value => @user.name %> <%= f.hidden_field :posterusername, :value => current_user.username %> <%= f.hidden_field :postercachedslug, :value => @user.cached_slug %> <div class="postbox"> <div class="field"> <%= f.text_area :content %> </div> <div class="submit"> <%= f.submit "Submit" %> </div> </div>

Feed Item view

<div class="profilepic"> <% if feed_item.user.profile_file_name == nil %> <%= link_to image_tag("thumb.jpeg"), feed_item.user %> <% else %> <%= link_to image_tag(feed_item.user.profile.url(:thumb)), feed_item.user %> <% end %> </div> <span class='feeditem'> <div class='row1'> <strong> <span class="user"><%= link_to feed_item.name, feed_item.user %> </span> </strong> <%= image_tag("arrow.png") %> <span class="name"><%= link_to feed_item.postername, feed_item.postercachedslug %></span> </div> <div class='row2'> <span class="content"><%= feed_item.content %></span> </div> <div class='row3'> <span class="timestamp"> <%= time_ago_in_words(feed_item.created_at) %> ago. </span> <% if current_user?(feed_item.user) %> <%= link_to "delete", feed_item, :method => :delete, :confirm => "You sure?" %> <% end %> </div> </span>

更多推荐

本文发布于:2023-04-21 18:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d0c29a06e8c398ce5aa3d22d4373cd80.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:该用户   模型   用户   ID   Posts

发布评论

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

>www.elefans.com

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