Ruby-on-rails 渲染:带有参数的动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3150641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
render :action with params
提问by Swann
I have one Class with 2 methods. The first method is called by the view with some GET parameters ( params[:page] ). I would like to save those params and send them by a render action to my second method.
我有一个带有 2 种方法的类。第一个方法由带有一些 GET 参数( params[:page] )的视图调用。我想保存这些参数并通过渲染操作将它们发送到我的第二种方法。
class exemple
def first
## sql save of params[:page]
render :action => "second"
end
def second
##
## Here I need my params[:page] to do paginate stuff
##
respond_to do |format|
format.html
end
end
end
So my question is : How can I send params with a render :action ?
所以我的问题是:如何使用 render :action 发送参数?
thanks :)
谢谢 :)
回答by Salil
render :action => "second"
When you render, then your method written in :actionis not called, only the view with that action name is called.
渲染时,:action不会调用写入的方法,只会调用具有该操作名称的视图。
In your example, when you render, then your method secondis not called but you are instead rendering the second.html.erbview.
在您的示例中,当您渲染时,second不会调用您的方法,而是渲染second.html.erb视图。
For more details refer to this.
有关更多详细信息,请参阅此。
To call that method you have to use redirect_to, which looks something like the following:
要调用该方法,您必须使用redirect_to,如下所示:
redirect_to :action => "second", :page=> 4

