当用户尝试使用 Devise 和 Rails 直接 link 广告#show 操作时要求登录 4
Ask for login when user tries direct link to advertisement#show action with Devise and Rails 4
我有两个不同的模型可以验证:user 和 advertisement
我在广告控制器中设置了这个:
before_action :authenticate_user!, only: [:show,:edit,:update]
我希望每次访问者尝试访问限制区域时都弹出登录表单。在这种情况下 advertisement#show
如果访问者尝试从索引页面访问显示操作,一切正常。
为此我设置如下:
在应用程序助手中:
def link_to(name = nil, options = nil, html_options = nil, &block)
html_options, options, name = options, name, block if block_given?
options ||= {}
html_options = convert_options_to_data_attributes(options, html_options)
url = url_for(options)
html_options['href'] ||= url if url.present?
if html_options["require_login"] and not user_signed_in?
html_options.delete "data-toggle"
html_options.delete "data-remote"
html_options.delete "data-target"
html_options.delete "data-method"
html_options['href'] = '#'
html_options['onclick'] = "loginModalShow();"
html_options['class'] = "" if html_options['class'].nil?
html_options['class'] = html_options['class']
end
if html_options["require_login2"] and not advertisement_signed_in?
html_options.delete "data-toggle"
html_options.delete "data-remote"
html_options.delete "data-target"
html_options.delete "data-method"
html_options['href'] = '#'
html_options['onclick'] = "advertisementModalShow();"
html_options['class'] = "" if html_options['class'].nil?
html_options['class'] = html_options['class']
end
content_tag(:a, name || url, html_options, &block)
end
应用布局:
<%= render partial: "shared/login" %>
在_login部分:
<% unless user_signed_in? %>
<script>
function loginModalShow() {
$('#loginmodal').modal('show') (e)
e.preventDefault();
}
</script>
<%end%>
<% unless advertisement_signed_in? %>
<script>
function advertisementModalShow() {
$('#advertisementmodal').modal('show') (e)
e.preventDefault();
}
</script>
<%end%>
我在应用程序布局中有这些模式。
问题:当访问者尝试使用直接 link 时,他被重定向到登录页面。我想不通,每次访问者尝试使用直接 link.
时如何显示登录弹出窗口
示例:http://localhost:3000/ru/advertisements/46
任何帮助都会很棒!
谢谢。
使用 before_action 它是 运行 在执行控制器显示操作之前的请求期间,从而重定向到登录页面。 link_to 工作的原因是因为您正在更改链接行为并且它没有点击 before_action 因为您指定了 only
过滤器。您可以删除之前的操作,并在您的视图中检查是否有 current_user
如果没有手动显示模态,否则照常进行。
我有两个不同的模型可以验证:user 和 advertisement
我在广告控制器中设置了这个:
before_action :authenticate_user!, only: [:show,:edit,:update]
我希望每次访问者尝试访问限制区域时都弹出登录表单。在这种情况下 advertisement#show
如果访问者尝试从索引页面访问显示操作,一切正常。
为此我设置如下:
在应用程序助手中:
def link_to(name = nil, options = nil, html_options = nil, &block)
html_options, options, name = options, name, block if block_given?
options ||= {}
html_options = convert_options_to_data_attributes(options, html_options)
url = url_for(options)
html_options['href'] ||= url if url.present?
if html_options["require_login"] and not user_signed_in?
html_options.delete "data-toggle"
html_options.delete "data-remote"
html_options.delete "data-target"
html_options.delete "data-method"
html_options['href'] = '#'
html_options['onclick'] = "loginModalShow();"
html_options['class'] = "" if html_options['class'].nil?
html_options['class'] = html_options['class']
end
if html_options["require_login2"] and not advertisement_signed_in?
html_options.delete "data-toggle"
html_options.delete "data-remote"
html_options.delete "data-target"
html_options.delete "data-method"
html_options['href'] = '#'
html_options['onclick'] = "advertisementModalShow();"
html_options['class'] = "" if html_options['class'].nil?
html_options['class'] = html_options['class']
end
content_tag(:a, name || url, html_options, &block)
end
应用布局:
<%= render partial: "shared/login" %>
在_login部分:
<% unless user_signed_in? %>
<script>
function loginModalShow() {
$('#loginmodal').modal('show') (e)
e.preventDefault();
}
</script>
<%end%>
<% unless advertisement_signed_in? %>
<script>
function advertisementModalShow() {
$('#advertisementmodal').modal('show') (e)
e.preventDefault();
}
</script>
<%end%>
我在应用程序布局中有这些模式。
问题:当访问者尝试使用直接 link 时,他被重定向到登录页面。我想不通,每次访问者尝试使用直接 link.
时如何显示登录弹出窗口示例:http://localhost:3000/ru/advertisements/46
任何帮助都会很棒!
谢谢。
使用 before_action 它是 运行 在执行控制器显示操作之前的请求期间,从而重定向到登录页面。 link_to 工作的原因是因为您正在更改链接行为并且它没有点击 before_action 因为您指定了 only
过滤器。您可以删除之前的操作,并在您的视图中检查是否有 current_user
如果没有手动显示模态,否则照常进行。