if current_user.id = @game.user_id

耗尽温柔 提交于 2019-12-12 03:21:46

问题


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

Any ideas?


回答1:


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

and then in your view :

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




回答2:


You want ==

So:

<% if current_user.id == @game.user_id %>


来源:https://stackoverflow.com/questions/9541761/if-current-user-id-game-user-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!