Action that refreshes the current page

允我心安 提交于 2020-01-14 14:02:27

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/8618877/action-that-refreshes-the-current-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!