【问题标题】:render :action with params渲染:带参数的动作
【发布时间】:2011-03-10 04:51:17
【问题描述】:

我有一个有 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

所以我的问题是:如何发送带有渲染的参数:动作?

谢谢:)

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 render params


    【解决方案1】:
    render :action => "second"
    

    当你渲染时,你写在:action中的方法不会被调用,只有那个动作名字的视图被调用。

    在您的示例中,当您渲染时,您的方法 second 不会被调用,而是您正在渲染 second.html.erb 视图。

    更多详情请参考this

    要调用该方法,您必须使用redirect_to,如下所示:

    redirect_to :action => "second", :page=> 4
    

    【讨论】:

    • 感谢您的回答! :) 我尝试使用 redirect_to 并且它正在工作,但我想将第一种方法的所有 GET 参数发送到第二种方法。你知道解决方案还是我必须一个一个地发送所有参数?
    猜你喜欢
    • 2014-07-08
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2012-11-07
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多