链接回表单之前访问过的页面 [英] Link back to page visited before form
问题描述
我有一个列表页面,然后是一个表单,然后是一个感谢页面.我需要在感谢页面上放置一个链接,将用户带回他们在表单之前所在的页面,该页面总是变化的.我试过用这个:
I have a listing page, then a form, then a thank you page. I need to put a link on the thank you page that takes the user back to the page they were on before the form which always varies. I've tried using this:
= link_to "Back", :back
但这只会让他们回到上一页,所以是表单.
But this only takes them back to the previous page, so the form.
推荐答案
好吧,您可以在表单页面中设置一个方法来收集该 url.基本思想是使用自定义会话变量来存储先前的 url 并将其保留到下一个会话.
Well, you can set a method in the form page to collect that url. The basic idea is to use a custom session variable to store previous url and keep it to next session.
假设你的表单的 action 是 SomeController#new,那么
Suppose your form's action is SomeController#new, then
class SomeController < ApplicationController
after_filter "save_my_previous_url", only: [:new]
def save_my_previous_url
# session[:previous_url] is a Rails built-in variable to save last url.
session[:my_previous_url] = URI(request.referer || '').path
end
end
然后在感谢页面,你可以通过
Then in the thank you page, you can get this my_previous_url
by
session[:my_previous_url]
这应该适合你的情况,前两页前的网址.
This should be able to suit your case, the previous url two pages ago.
免责声明:未经证实.只是想法.
Disclaimer: This is not verified. Idea only.
会话属于控制器.它不是您可以直接在视图中使用的助手.您需要在控制器中定义一个实例变量,然后才能在视图中使用它.像这样
Session belongs to controller. It is not a helper you can use directly in view. You need to define an instance variable in controller and then you can use it in view. Like this
# Controller
@back_url = session[:my_previous_url]
# View
<%= link_to "Back", @back_url %>
这篇关于链接回表单之前访问过的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!