if current_user.id = @ game.user_id [英] if current_user.id = @game.user_id

查看:168
本文介绍了if current_user.id = @ game.user_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有链接到我项目中每个游戏的节目页面,如果
游戏user_id与当前登录用户的id匹配,那么我想要
它显示编辑按钮,如果它们是然后它不应该显示。

I have links to the show pages for each game in my project and if the games user_id matches the id of the currently signed in user then I want it to display the edit button if they are not then it shouldn't display.

我目前有以下代码集,但它不起作用。每个游戏
都有编辑按钮显示。代码如下:

I currently have the following code set but it doesn't work. Every game has the edit button display. The code is as followed:

 <% if current_user.id = @game.user_id %>
    <div id="text3"><%= link_to 'Edit', edit_game_path(@game) %></div><br />
 <% end %>

任何想法?

推荐答案

MrDanA的回答很可能是错误,但您可能希望更好地使用此代码。像那样检查不是Rails的做法。相反,制作一个用户实例方法,如:

MrDanA's answer is most probably the error, but you may want to make this code better. Checking like that is not the Rails way of doing it. Instead, make a User instance method like :

def has_game?(game)
  self.games.exists?(:id => game.id)
end

然后在您的视图中:

<% if current_user.has_game?(@game) %> ...

(甚至可以通过进一步委托存在于游戏模型中作为范围或更好所以,如果你愿意的话)

(can even be better by further delegating exists into the game model, as a scope or so, if you like)

这篇关于if current_user.id = @ game.user_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆