简体   繁体   English

我如何使用模态和远程形式使before_action:logged_in_user工作

[英]how do I make before_action :logged_in_user work with a modal and remote form

For example if I have the following code: 例如,如果我有以下代码:

class PhotosController < ApplicationController
  before_action :logged_in_user
end

class UsersController < ApplicationController
  def logged_in_user
    unless logged_in?
      store_location
      flash[:danger] = "Please log in or register"
    redirect_to login_url
  end
end

when a web user navigates to any of the photo routes: example.com/photos , example.com/photos/new , example.com/photos/1 they will be redirected_to the login_url page if they were not already logged_in. 当Web用户导航到任何照片的路线: example.com/photosexample.com/photos/newexample.com/photos/1他们将redirected_tologin_url页面,如果他们不是已经LOGGED_IN。

but what if I am opening up a modal from the homepage, example.com that contains a remote form_for @photo? 但是,如果我要从主页example.com中打开一个包含remote form_for @photo?的模式,该form_for @photo? it will not allow a web user who is not logged_in to submit the form, and upon submitting the form, they will then be redirected_to the login_url however this is far from ideal and I want them to be redirected immediately. 它将不允许login_url的Web用户提交表单,并且在提交表单后,他们将被redirected_tologin_url但这远非理想,我希望他们立即被重定向。

In your views; 在您看来; I think you could put this remote form in a partial, Then render it if the user logged in, It should be something like that 我认为您可以将此远程表单放在局部中,然后在用户登录时呈现它,应该是这样的

homepage.html.erb homepage.html.erb

<% if logged_in? %>
  <%= render 'form_partial' %>
<% else %>
  <%= render 'somthing_else' %>
<% end %>

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

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