我有两个不同的模型可以验证:用户和广告。
我已在广告控制员中设置了这样的系统:
before_action :authenticate_user!, only: [:show,:edit,:update]我想有弹出式的登录表单,每次visititor试图访问限制区域。在本例中,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%>我在应用程序布局中有这些模式。
Problem:当访问者尝试使用直接链接时,他被重定向到登录页面。我搞不懂,每次访问者尝试使用直接链接时,如何显示登录弹出。
示例:http://localhost:3000/ru/advertisements/46
任何帮助都会很好!
谢谢。
发布于 2015-01-19 02:00:12
使用before_action,它将在执行控制器显示操作之前在请求期间运行,从而重定向到登录页面。您的link_to工作的原因是您正在更改链接行为,而不是因为您指定了only筛选器而击中了only。您可以删除“前”操作,并在视图中检查是否存在current_user,如果不手动显示该模式,则继续进行正常操作。
https://stackoverflow.com/questions/28014118
复制相似问题