我有两个控制器,它们与我试图整合/“干燥”/重构/或其他任何东西基本相同。我搜索并找到了各种解决方案,但似乎无法实现其中任何一个。
需要合并的两个控制器的示例代码:
class ClientsController < ApplicationController
before_filter :signed_in_user, only: [:show, :index, :edit, :update]
def update
@client = Client.find(params[:id])
if @client.update_attributes(params[:client])
flash[:success] = "Client updated"
redirect_to @client
else
render 'edit'
end
end
end
~
class ItemCatsController < ApplicationController
before_filter :signed_in_user, only: [:show, :index, :edit, :update]
def update
@item_cat = ItemCat.find(params[:id])
if @item_cat.update_attributes(params[:item_cat])
flash[:success] = "Item Category updated"
redirect_to @item_cat
else
render 'edit'
end
end
end
我怀疑我需要做的是创建一个超级控制器,但我一直在寻找一种通用的方法来引用模型。任何帮助将不胜感激。
注意:我真的很想避免添加任何额外的宝石/插件来使其工作;我更喜欢“手动”/非魔法解决方案,以便我能理解到底发生了什么。