[英]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.