ruby简单增删改查的实现

<%=manage_meeting_service(@notice_types,"notice_type","通知类型管理")%>

module MeetingService

def manage_services(me_name)
class_name=me_name.to_s.camelize.constantize
define_method("manage_#{me_name}s") do
instance_variable_set("@#{me_name}s",class_name.all )
end
define_method("delete_#{me_name}") do
@cate=class_name.find(params[:id]).delete
redirect_to :action=>"manage_"+me_name.to_s+"s"
end
define_method("edit_#{me_name}") do
@cate=class_name.find(params[:id])
if me_name.to_s == "notice_type"
render :template=>"/notices/edit_notice_type.html.erb"
else
render :template=>"/rcrm_meetings/edit_service.html.erb"
end
end
define_method("update_#{me_name}") do
@cate=class_name.find(params[:id])
@cate.update_attributes(:name=>params[:name])
redirect_to :action=>"manage_"+me_name.to_s+"s"
end
define_method("add_#{me_name}") do
new_name_module class_name
redirect_to :action=>"manage_"+me_name.to_s+"s"
end
end

end

你可能感兴趣的:(RUBY,元编程,ruby)