Posts模型中保存的用户ID,如何呈现该用户?

时间:2011-05-22 01:08:39

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-plugins

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

更多信息:

  1. 使用Slugged gem
  2. 尝试生成帖子提要,当我在我的视图中调用feed_item.user时,它会将我引导给发布帖子的用户,但不会引导给发布帖子的用户。
  3. Feed Item View

    enter image description here

    Feed视图

    enter image description here

    页面控制器主页功能

    enter image description here

    @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
    

2 个答案:

答案 0 :(得分:4)

你的方法有点奇怪,因为你使用属性:poster而不是:user_id这是铁路惯例

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

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

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

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

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

答案 1 :(得分:2)

最终,我决定以一种非常有效的方式进行,只是为了让功能正常运行。虽然我怀疑,如果你能以某种方式将用户ID绑定在:海报中,设置了一个新的模型(有许多,属于等等......),那么它的工作方式会好得多。 (我相信这是stephenmurdoch的回答尝试,只有一个问题:user_id已经绑定到发布用户。)

我在制作每个帖子时保存了更多用户信息,然后为每个Feed项呈现了个人资料帖子接收者的信息。另外,由于用户拥有基于cached_slug的唯一网址,因此我可以使用&lt;%= link_to feed_item.postername,feed_item.postercachedslug%&gt;创建发布帖子的个人资料的链接。

发布表单视图

<%= 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>