Rails 3アプリには、ブールフィールドを持つモデルがいくつかありますdisabled
。これらのモデルのコントローラーでは、カスタムアクションを使用して、 Ajaxを使用しdisable
てフィールドを切り替えます。disabled
例(クライアントの場合)、
# routes.rb
resources :clients do
member do
get :toggle_disable, to: 'clients#disable', as: :disable
end
end
# clients_controller.rb
def disable
@client = Client.find(params[:id])
@client.update_attribute :disabled, !@client.disabled
render 'clients/update_client', format: :js
end
# update_client.js.erb
$('#client-<%= @client.id %>-details').html("<%= escape_javascript(render 'clients/client', client: @client) %>");
私のアプリケーションには、少なくとも10個のリソース用のこのコードがあります。
質問
このコードを乾燥させ、これらのブールフィールドのアクションを動的に追加するにはどうすればよいですか?親コントローラーまたはモジュールを作成することもできましたが、ビューコードをどのように処理するかわかりません。
私はこのようなことをすることができるはずです
#clients_controller.rb
class ClientsController < ApplicationController
add_toggle_action :disable
end