Rails 将查看链接重定向到用户想要在登录前查看的页面 [英] Rails to redirect View link to page user wanted to view prior to login

查看:46
本文介绍了Rails 将查看链接重定向到用户想要在登录前查看的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 的新手,我有一个这样的 HTML.erb

I am new to rails I have an HTML.erb like this

<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>

在这里,我想重定向到用户登录后想要查看的实际书籍页面,如果用户未登录,则当用户单击查看按钮时,登录弹出窗口将出现.那么我该怎么做.对于我在 Applications_controller 中编写的菜单屏幕的正常登录

Here I wanted to redirect to the actual books page the user wanted to view after user login where the login popup will come when user clicks the view button if user is not logged in. So how can I do it. For my Normal login from the menu screen I have written in applications_controller has

这是我的应用程序控制器

This is my application controller

def after_sign_in_path_for(resources)
        if current_user.profile.blank?
            new_profile_path 
        else
            session[:previous_url] || books_path
        end
    end

所以当用户点击查看按钮时,它应该重定向到他登录后想要查看的实际页面.任何人都可以告诉我如何做到这一点.我正在使用设备进行身份验证

So when user clicks on view button it should redirect to actual page he wanted to view after login. Can any tell me how to do this. I am using devise for authentication

这是我的书控制器

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

show.html.erb

show.html.erb

<p id="notice"><%= notice %></p>

<p>
 <h1> <b>Title:</b>
  <%= @book.name %>
</p></h1>
<p>
  <b>Logo:</b><br>
  <%=image_tag @book.logo.url(:thumb)%>
</p>
<p>
 <h2> <b>Author:</b></h2>
 <h2> <%= @book.author %></h2>
</p>

<body>
  <b>Description:</b>
 <h3> <%= @book.Description %></h3>

  <h3>Current User Satisfaction:- <%= @current_user_satisfaction %> <br/></h3>


<h2><b>Rating:</b></h2>
<table id="book" border="3" layout= "fixed" width=" 100%"  >
  <colgroup>
    <col style="width: 33%" />
    <col style="width: 33%" />
    <col style="width: 33%" />

  <tr>
    <th>Quality:- </th>
    <th>satisfaction:-</th>
    <th>does it cover all:- </th>

  </tr>
  <tr >
   <td><%= @avg_r1.shift %></td>
    <td><%= @avg_r2 .shift %></td>
    <td><%= @avg_r3.shift   %></td>


  </tr>

 </colgroup>
</table>






<br /><br />

 |
<%= link_to 'Back', books_path %>
</body>

错误

 Started GET "/books_ratings/user_rating?book_id=1" for 127.0.0.1 at 2014-01-19 17:33:27 +0100
    Processing by BooksRatingsController#user_rating as */*
      Parameters: {"book_id"=>"1"}
    Completed 500 Internal Server Error in 1ms

    NoMethodError (undefined method `profile' for nil:NilClass):
      app/controllers/book_ratings_controller.rb:72:in `user_rating'

我的 booksratings_controller 第 72 行

My booksratings_controller line 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

推荐答案

我通过在应用程序控制器中添加路径来解决问题

I have solved by problem by adding the path in the applications controller as

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

这篇关于Rails 将查看链接重定向到用户想要在登录前查看的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆