简体   繁体   English

当前用户信息显示在所有概要文件上Ruby on Rails

[英]Current user information is displaying on all profiles Ruby on Rails

So I am making a Social media website where user join events and compete against each other. 因此,我正在建立一个社交媒体网站,用户可以在其中加入活动并相互竞争。

Each time a user join an event a status appears on his profile. 每次用户加入事件时,状态都会显示在其个人资料上。 The issue is that when I look at other user profiles, the statuses just match who ever the current user is. 问题是,当我查看其他用户配置文件时,状态仅与当前用户的身份匹配。

If someone could help me write some code that shows the different statuses for each of the users that would be great. 如果有人可以帮助我编写一些代码,为每个用户显示不同的状态,那将是很好的。 I know the question may sound a little vague. 我知道这个问题听起来有点含糊。

I just need to know a way to see only post based on which profile I am viewing not the current user. 我只需要知道一种方法即可根据我正在查看的个人资料而不是当前用户只查看帖子。 Thanks! 谢谢!

<div class="center">
  <div class="col-xs-6">
    <div class="panel panel-default">
      <div class="panel-body">
        <% if !@user_events.present? %>
          <h4>Hi <%= User.find_by_username(params[:id]).username %>, thanks for joining!</h4>
          <h4>Join events, complete them, and compete</h4>
        <% end %>

        <% @user_events.each do |ue|%>
          <% if ue.event.present? %>
            <h4 class="blue">
              <%= User.find_by_username(params[:id]).username %>
            </h4>
            <% userevent = User.find_by(params[:event]) %>
            <p>I'm going to <%= link_to ue.event.title %>.</p>
            <% if ue.hours.present? %>
              <h5> Completed Hours: <%= ue.hours %>   </h5>
            <% end %>
            <% if ue.hours.present? %>
              <img src="#" style="width: 10%;" alt="">
            <% end %>
          <% end %>
        <% end %>
      </div>
    </div>
  </div>
</div>

First of all, it's very bad to have queries to the database inside of your view. 首先,在视图内部查询数据库是非常糟糕的。 Please, move expression inside <%= User.find_by_username(params[:id]).username %> to your controller and then use instance variable to show username. 请将<%= User.find_by_username(params[:id]).username %>中的表达式移至控制器,然后使用实例变量显示用户名。

It would be great if you provide your controller code too. 如果您也提供控制器代码,那就太好了。 I guess you should pick user events depending on the user you found by params[:id]. 我猜您应该根据params [:id]找到的用户来选择用户事件。

Without the controller, it is a bit hard to help. 没有控制器,很难提供帮助。 But I would suggest trying something on the user controller like 但我建议在用户控制器上尝试一些操作,例如

def get_status_by_user
  user_id = params[:user_id]
  user = User.find_by(id: user_id)
  user.event
end

where the params[user_id] is the id of hte user you want to get the event. 其中params[user_id]是您要获取事件的用户的ID。

Also what I can see is <% userevent = User.find_by(params[:event]) %> you do not specify a user so the method will return the last one 另外我看到的是<% userevent = User.find_by(params[:event]) %>您未指定用户,因此该方法将返回最后一个

Finds the first record matching the specified conditions. 查找符合指定条件的第一条记录。

Find_by docuemntaion Find_by文档

So if you want to keep your code mostly as it is you need to first find the user you want to get the event and then get the status for that specific user 因此,如果您想保留大部分代码,则需要先找到要获取事件的用户,然后获取该特定用户的状态

Also giving a little information on the DB schema could help 还提供一些有关数据库架构的信息可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM