简体   繁体   English

在Rails中将实例变量从一种形式传递到另一种控制器的动作

[英]Passing instance variable from one form to the a different controller's action in Rails

Simple question - I have a form where I create an instance of an object. 一个简单的问题-我有一个创建对象实例的表单。 After I create that object (aka submit the form), I want to redirect to a new form that is associated with a different controller's action and carry that instance variable to populate some of that form's fields. 创建该对象(也就是提交表单)之后,我想重定向到与其他控制器的动作相关联的新表单,并携带该实例变量来填充该表单的某些字段。

I know I typically have 2 options, store that instance variable in the session or pass it via params. 我知道我通常有2个选项,将实例变量存储在会话中或通过params传递。 I can't use sessions (for a variety of reasons I won't bore you with). 我无法使用会话(出于多种原因,我不会厌烦您)。 The params option I am confused on. 我对params选项感到困惑。

I should know this. 我应该知道这一点。 :( How would you go about doing this? Any examples greatly appreciated!! :(您将如何进行?任何示例都将不胜感激!!

Betsy 贝齐

You'll have two methods on your controller. 您的控制器上将有两种方法。 One for each form (rendered by the associated template). 每个表单一个(由关联的模板呈现)。 The first form should post to the second action. 第一种形式应发布到第二种行动。 The second action can then transfer the request parameters into instance variables, to be available within the second template. 然后,第二个动作可以将请求参数传输到实例变量中,以在第二个模板中可用。

class FooController
  def bar
   # setup instance variables and render first form
  end

  def baz
    @bar_values = params[:bar]
    # setup other instance variables and render second form
  end
end

UPDATE0 Do it across two controllers using session. UPDATE0使用会话跨两个控制器执行此操作。

class FooController
  def new_baz
    # setup instance variables and render the first form
  end

  def create_baz
    # respond to posting of form data
    session[:current_baz_values] = params
    redirect_to :action => "baq", :controller => "bar"
  end
end

class BarController
  def baq
    @baz_values = session[:current_baz_values]
    # setup other instance variables and render the second form
  end
end

Could you somehow just do a find of the newly created record in the other controller, and then use that to populate the info you need? 您能以某种方式在另一个控制器中找到新创建的记录,然后使用它来填充所需的信息吗?

Also, unless you are using AJAX you usually don't want to have modification actions on the show page for a record. 另外,除非您使用AJAX,否则通常不希望在显示页面上进行记录的修改操作。 Those belong on the edit or update page. 它们属于编辑或更新页面。 If you always want people to be able to edit a record on the same page I would either use some AJAX on the show page, or just always return the edit/update page instead... 如果您始终希望人们能够在同一页面上编辑记录,则可以在显示页面上使用一些AJAX,或者总是返回编辑/更新页面...

If you do not want to use sessions, you could use the flash variable to store your parameter. 如果不想使用会话,则可以使用flash变量存储参数。 Something like, flash[:my_params] = params, and then reading it back in the next request with params = flash[:my_params]. 类似flash [:my_params] = params,然后在下一个请求中使用params = flash [:my_params]读回它。 The good thing about flash, is that persists for only the next request, and auto-clears after that. 关于flash的好处是,它仅对下一个请求持续存在,并在此之后自动清除。

If you are looking for passing values from the client side when using Ajax, then probably setting a hidden field with the parameters is going to pass them on to the next request. 如果您在使用Ajax时正在寻找从客户端传递值的方法,那么可能设置带有参数的隐藏字段会将它们传递给下一个请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 3:使用render动作将实例变量从控制器传递到不同的视图 - Rails 3: Passing an instance variable from a controller to a different view using a render action 在Rails中将实例变量从一个控制器传递到另一个控制器 - Passing an instance variable from one controller to another in rails 将表单从一个控制器的视图传递给一个完全不同的控制器 - Passing a form from one controller's view to a completely different controller 从控制器传递实例变量以在Rails中进行查看 - Passing instance variable from controller to view in rails Rails:通过部分传递实例变量到不同的控制器 - Rails: passing an instance variable to different controller through a partial 在Rails的redirect_to中将模型实例变量从一个控制器传递到另一个控制器 - Passing model instance variable from one controller to another in redirect_to in Rails 创建操作无法保存到数据库-将参数从一种形式传递到另一种控制器的形式 - Create Action Can;t Save To Database — Passing parameters from one form to another controller's 在Rails中将实例变量从Model传递到View到Controller - Passing instance variable from Model to View to Controller in Rails ruby on rails将实例变量从视图传递给控制器 - ruby on rails passing an instance variable from view to controller Rails通过不同对象的形式将参数从一个控制器动作传递到另一个 - Rails pass parameter from one controller action to another through form of different object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM