刷新当前页面的操作

发布于 2024-12-22 19:31:39 字数 420 浏览 0 评论 0原文

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

示例:
控制器:A
视图: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是否可以在链接中传递参数来指示必须呈现的视图?

谢谢

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

Example:
Controller: A
Views: A, B.

controller A is the following:

def action1
    somethingToTheDB
end

view A is the following:

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

view B is the following

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

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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗喜 2024-12-29 19:31:39

不确定是否明白您的问题,但您不能这样做:

def action_1
  # something_to_the_db
  redirect_to :back
end

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

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

aktionkontroller 没有拼写错误。你必须这样写,否则会发生冲突。

action_namecontroller_name 是变量。像这样写它们。

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

<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

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

def action_1
  # something_to_the_db
  redirect_to :back
end

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

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

action_name and controller_name are variables. Write them like that.

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>

Then in your controller:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文