Sus*_* Kr 0 forms controller ruby-on-rails
在我的项目中,我有两个叫做PropertiesController和的控制器FacilitiesController.创建一个propertyin之后PropertiesController,我想加载view(form)from FacilitiesController.
我尝试使用render facilities/new显示错误(表单中的第一个参数不能包含nil或为空),因为我没有初始化传递给form_for方法的参数(@facility).
我可以通过初始化@facility变量来避免上述错误PropertiesController.但我在form_for其中的代码中调用了object当前的方法FacilitiesController.我不希望所有代码再次重复PropertiesController.
如何在FacilitiesController不重复代码的情况下渲染视图?
我希望下面的代码适合你
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
Run Code Online (Sandbox Code Playgroud)