(configuration_controller에) 당신이 작업에 대한 모든 4 개 모델의 매개 변수를 전달할 수 있다면, 당신이 좋아하는 뭔가를 할 수 : 당신이 행동 atomicity을 할 수 있습니다로
# app/controllers/configuration_controller.rb
def update
@a = ModelA.find(params[:model_a][:id])
@b = ModelB.find(params[:model_b][:id])
@c = ModelC.find(params[:model_c][:id])
@d = ModelD.find(params[:model_d][:id])
@a.assign_attributes(model_a_params)
@b.assign_attributes(model_b_params)
@c.assign_attributes(model_c_params)
@d.assign_attributes(model_d_params)
respond_to do |format|
if @a.save && @b.save && @c.save && @d.save
format.html { redirect_to :back, notice: "" }
format.json { head :no_content }
else
format.json { render json: [@a.errors, @b.errors, @c.errors, @d.errors], status: :unprocessable_entity }
format.html { render :show }
end
end
private
def model_a_params
params.require(:model_a).permit(:id) # and other permitted params
end
#... same for the other 3 models
나는 여기 assign_attributes
을 사용했다.
[편집] 다른 한편으로는, 당신은 별도의 행동의 모델을 업데이트하려면, 경우
는, 단순히 컨트롤러에 4 개 별도의 조치가 : 물론
# app/controllers/configuration_controller.rb
def update_model_a
@a = ModelA.find(params[:id])
respond_to do |format|
if @a.save
format.html { redirect_to :back, notice: "" }
format.json { head :no_content }
else
format.json { render json: [@a.errors, @b.errors, @c.errors, @d.errors], status: :unprocessable_entity }
format.html { render :show }
end
end
def update_model_b
@b = ModelB.find(params[:id])
respond_to do |format|
if @b.save
format.html { redirect_to :back, notice: "" }
format.json { head :no_content }
else
format.json { render json: [@a.errors, @b.errors, @c.errors, @d.errors], status: :unprocessable_entity }
format.html { render :show }
end
end
# ... etc.
private
def model_a_params
params.require(:model_a).permit(:id) # and other permitted params
end
def model_b_params
params.require(:model_b).permit(:id) # and other permitted params
end
# ... etc.
#... same for the other 3 models
당신이 할 수있는을 같은 형식으로 다른 모델에 속하는 다른 매개 변수를 수집하십시오. 그리고 하나의 컨트롤러 동작 (#create 또는 #update) 내에서 4 가지 모델을 업데이트하십시오. 그것은 특별한 해킹이 아니며 컨트롤러에서 모든 모델의 레코드를 호출하는 기능입니다. 중첩 된 양식을 사용할 수 있습니다. 또는 아무런 문제없이 수집 한 다음 컨트롤러 작업 내에서 수동으로 할당하십시오. – Maxence
이러한 모델이 관련되어 있는지 여부에 따라 [중첩 된 특성] (http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)을 사용할 수 있습니다. –