简体   繁体   中英

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.

but what if I am opening up a modal from the homepage, example.com that contains a remote 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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