Ruby on rails Rails将视图链接重定向到用户登录前想要查看的页面
我是rails新手,我有一个像这样的HTML.erbRuby on rails Rails将视图链接重定向到用户登录前想要查看的页面,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我是rails新手,我有一个像这样的HTML.erb <h1>books List</h1> <table id="book" border="3" layout= "fixed" width=" 100%" > <tr> <th>Name</th> <th></th> <th></th> <th></th>
<h1>books List</h1>
<table id="book" border="3" layout= "fixed" width=" 100%" >
<tr>
<th>Name</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @books.each do |book| %>
<tr >
<td><%= book.name %></td>
<td><%= link_to 'VIEW', book %></td>
</tr>
<% end %>
</table>
所以,当用户点击查看按钮时,它应该重定向到登录后他想要查看的实际页面。能告诉我怎么做吗。我正在使用Desive进行身份验证
这是我的图书管理员
class BooksController < ApplicationController
before_action :authenticate_user! , :only => [:show],
#:except => [:index]
def index
@books = BOOK.all.order('rank DESC NULLS LAST')
end
def show
@book = BOOK.find(params[:id])
#@article.user_id = current_user.id
book_rating = BooksRating.where( :book_id =>@book.id,
:user_id => current_user.id )
@avg_r1 = book_rating.collect(&:r1)
@avg_r2 = book_rating.collect(&:r2)
@avg_r3 = book_rating.collect(&:r3)
arr= @avg_r1+@avg_r2+ @avg_r3
@current_user_satisfaction = arr.flatten.compact.sum / arr.flatten.compact.size if arr.flatten.compact.present?
end
def new
@book = BOOK.new
end
def create
@book= Book.new(comp_params)
if @book.save
flash[:success] = "Welcome to my app"
redirect_to root_url
else
render 'new'
end
end
private
def book_params
params.require(:book).permit(:name, :place, :Description,:logo)
end
end
我的笔记本电脑控制器第72行
def user_rating
if current_user
book_id = current_user.profile.books.map(&:id)
#rating = current_user.book_ratings.find(:all,:conditions=>["user_id = ? and book_id IN (?)",current_user.id,book_id])
rating = current_user.book_ratings.where("user_id = ? and book_id IN (?)",current_user.id,book_id)
if rating.size !=book_id.size
if session[:book_id].blank?
session[:book_id] = params[:book_id]
render :json=>"redirect".to_json
else
book_id = book_id - rating.map(&:book_id)
@book_rating = current_user.book_ratings.new(:book_id=>book_id.first)
@book = Book.find(book_id.first)
session[:book_id] = params[:book_id]
render :layout =>false
end
else
session[:book_id] = nil
render :json => "redirect".to_json
# redirect_to book_path(params[:book_id])
end
假设你正在使用Deave
session[:previous_url]
包含以前的url
所以你可以这样做
def after_sign_in_path_for(resources)
if current_user.profile.blank?
new_profile_path
else
session[:previous_url] || root_path
end
end
因此,如果没有以前的url,它将重定向到以前的url或根路径
编辑
在您的书本顶部_controller.rb
before_filter :authenticate_user!, :only => [:show]
现在,当任何人试图进入图书视图的显示时,他们将被要求登录,登录后,他们将被重定向到新的_profile_路径,即他们试图查看的图书,如果他们进入网站时碰巧直接进入登录页面,他们将被重定向到根路径
第二次编辑
下面的代码令人困惑,如果用户登录,您将显示一个名为“VIEW”的书籍链接,否则“VIEW”是指向新用户会话路径的链接。现在,如果用户未登录,他们将看到登录路径的“视图”,并且在登录后将返回索引,因为您已将他们从索引视图发送到登录操作
<% @books.each do |book| %>
<tr >
<td><%= book.name %></td>
<% if !current_user.blank? %>
<td><%= link_to 'VIEW', book %></td>
<%else%>
<td><%= link_to 'VIEW', new_user_session_path,:class=>"login" %></td>
<%end %>
</tr>
<% end %>
结尾不应该有逗号
before_filter :authenticate_user!, :only => [:show]
如果您使用Desive gem进行用户身份验证,那么只需使用
before_filter :authenticate_user!
或者,如果您已经创建了自己的身份验证系统,则在登录操作中设置此会话:
def new
session[return_url] = request.env['HTTP_REFERER']
end
然后在登录之后
def after_sign_in_path_for(resources)
if session[:return_url]
return = session[:return_url]
session[:return_url]=nil
return
elsif current_user.profile.blank?
new_profile_path
else
books_path
end
end
我已经通过在应用程序控制器中添加路径解决了这个问题
def after_sign_in_path_for(resources)
if current_user.profile.blank?
new_profile_path
else
#session[:previous_url] || books_path
book_path({:id=>params[:book_id]})
end
end
不,它仍在加载到索引页。上一个url始终是登录前的url,因此,如果用户位于索引页,而您将其重定向到登录,则他们将被重定向到索引。但是如果他们在一个显示视图中,用户在登录inNo后将被重定向到显示视图我不想那样,我在索引页中尝试的是我有一些视图按钮,这些按钮将重定向到下一页用户想要查看的内容,但要查看该特定页面,用户将被要求登录,然后重定向到该页面。例如,如果用户单击了重定向到book/1页面的视图,则在请求登录弹出窗口之后,应重定向到books/1页面。相同的视图->书籍/3->询问登录弹出窗口->书籍/3是如果在书籍上放置一个前文件管理器,则会发生这种情况:显示操作,然后,当用户尝试进入书展页面时,他们将被要求登录并重定向到特定的书展视图。是的,我保留了相同的视图,但它仍重定向到正常的索引页面。您的前文件器末尾有一个多余的逗号,不应该在那里。我已删除了逗号,但它不起作用。请继续具体发生了什么,如何发生?什么时候您是否收到任何错误消息?如果是这样的话,发布它们,否则当你只说它不工作时,它真的很难帮助你。实际上链接视图不工作,当我点击它时它处于非活动状态。我没有收到任何错误信息。只有当我登录并点击查看时,它才起作用。在索引视图下面还有两行
before_filter :authenticate_user!, :only => [:show]
before_filter :authenticate_user!
def new
session[return_url] = request.env['HTTP_REFERER']
end
def after_sign_in_path_for(resources)
if session[:return_url]
return = session[:return_url]
session[:return_url]=nil
return
elsif current_user.profile.blank?
new_profile_path
else
books_path
end
end
def after_sign_in_path_for(resources)
if current_user.profile.blank?
new_profile_path
else
#session[:previous_url] || books_path
book_path({:id=>params[:book_id]})
end
end