简体   繁体   English

仅在用户登录后如何使AJAX工作? -轨道

[英]How to make an AJAX work only if the user is logged in? -rails

I have an app that the user can only click on a photo and save them if he is logged in, however he is still able to click on it and save them when logged out because the show page is similar. 我有一个应用程序,用户登录后只能单击照片并保存,但是由于显示页面相似,他仍然可以单击照片并在注销后保存。

If he is logged out though, the alert "picture was saved" still pops up. 如果他已注销,则仍会弹出“图片已保存”警报。

Any ideas? 有任何想法吗?

You should have a conditional statement in the button or link that states only if the user is logged in is he or she allowed to save something. 您应该在按钮或链接中有一个条件语句,该条件语句仅在用户登录后才允许他或她保存内容。 I don't have any code to use so I'll make up an example 我没有任何代码可使用,因此我将组成一个示例

<img>
  <%if user_logged_in%>
    <%= link_to "Save Picture", profile_picture_path(@profile)%>
  <% end %>
</img>

*Note: The user_logged_in is not a default helper method, I used it to better explain the example. *注意:user_logged_in不是默认的帮助器方法,我用它来更好地说明示例。 You would need to write that method using sessions. 您将需要使用会话编写该方法。

The problem is not in your AJAX call judging from your question, that seems to do exactly what your telling it to. 从您的问题来看,问题不在您的AJAX调用中,这似乎完全可以告诉您。 It's actually the fact that you aren't preventing the non-logged in user saving a picture. 实际上,您并没有阻止未登录用户保存图片。

With that being said, how is a user saving a picture if they aren't logged in? 话虽如此,如果用户未登录,该如何保存图片? Where and how is that picture being stored. 该图片存储在何处以及如何存储。 Please upload some code next time. 请下次上传一些代码。

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

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