刷新当前页面的操作 [英] Action that refreshes the current page

查看:110
本文介绍了刷新当前页面的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Rails控制器中创建一个动作,该动作在数据库中执行某项操作,然后仅刷新当前页面.

I want to create an action in a Rails controller that does something in the db and then just refreshes the current page.

示例:
控制器:A
浏览次数:A,B.

Example:
Controller: A
Views: A, B.

控制器A如下:

def action1
    somethingToTheDB
end

视图A如下:

-html-
-body--link to action 1--/body-
-/html-

B视图如下

-html-
-body--link to action 1--/body-
-/html-

如果我从视图AI转到动作1要刷新视图A,如果我从视图2来刷新视图2,是否可以在链接中不传递参数来表示必须渲染的视图的情况下进行?

If I come to action 1 from view A I want to refresh view A, if I come from view 2 I want to refresh view 2. Is that possible without passing a parameter in the link to indicate the view that must be rendered?

谢谢

推荐答案

不确定要回答您的问题,但您不能这样做:

Not sure to catch your question but can't you just do:

def action_1
  # something_to_the_db
  redirect_to :back
end

如果出于某种原因需要访问action_1中的controller_nameaction_name:

If you need to access your controller_name and action_name in your action_1 for whatever reason:

<%= link_to 'Refresh' refresh_path(:aktion => action_name, 
                                   :kontroller => controller_name) %> 

aktionkontroller不是错字.您必须这样写它们,否则会发生冲突.

aktion and kontroller aren't typo. You have to write them like that, otherwise, it will clash.

action_namecontroller_name是变量.这样写.

它看起来像这样(取决于您的refresh_path,当前控制器和当前操作):

It will look like something like that (depending on what's your refresh_path, your current controller and your current action):

<a href="/refresh?aktion=index&kontroller=articles">Refresh</a>

然后在您的控制器中:

def action_1
  kontroller = params[:kontroller]
  aktion     = params[:aktion]
  # Do whatever you want in the db
  redirect_to :controller => kontroller, :action => aktion
  # or redirect_to :back (better IMO)
end

这篇关于刷新当前页面的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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