[英]How to render view from of a different controller?
In my project I have two controllers called PropertiesController
and FacilitiesController
. 在我的项目中,我有两个名为PropertiesController
和FacilitiesController
控制器。 After creating a property
in PropertiesController
, I want to load the view(form)
from FacilitiesController
. 创建之后property
在PropertiesController
,我要加载的view(form)
从FacilitiesController
。
I tried to do using render facilities/new
which shows an error(First argument in form cannot contain nil or be empty), as I have not initialized the argument(@facility) passed to form_for method. 我尝试使用显示错误的render facilities/new
来做(表单中的第一个参数不能包含nil或为空),因为我尚未初始化传递给form_for方法的参数(@facility)。
I can avoid the above error by initializing the @facility variable in PropertiesController
. 我可以通过在PropertiesController
初始化@facility变量来避免上述错误。 But I have code in the form_for
which calls method on the object
present in FacilitiesController
. 但是我在form_for
有代码,它对FacilitiesController
存在的object
调用方法。 I don't want all that code to be duplicated again in PropertiesController
. 我不希望所有这些代码再次在PropertiesController
重复。
How should I render the view from FacilitiesController
without duplicating the code? 如何在不复制代码的情况下从FacilitiesController
呈现视图?
I hope below code work for you 我希望下面的代码为您工作
class PropertiesController < ApplicationController
def create
@property = Property.new(property_params)
respond_to do |format|
if @property.save
format.html { redirect_to facility_new_path, notice: 'Property was successfully created.' }
else
format.html { render action: 'new' }
format.json { render json: @property.errors, status: :unprocessable_entity }
end
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.