As part of learning Rails framework, I have small app in progress. Basically idea is, user can create IP address, define is it in use, to which server is assigned, which user is it using and role of IP address in web hosting environment. I have 3 models, IpAddress, IpAddressRole and IpAddressUser. User is able to define IpAddressRole and IpAddressUser from prepopulated postgresql database.
Main controller is IpAddressesController, which is responsible for creating, showing and other CRUD operations on primary model.
ip_addresses_controller.rb code is here:
class IpAddressesController < ApplicationController def index @ip_addresses = IpAddress.all.order(:title) end def show @ip_address = IpAddress.find(params[:id]) end def new @ip_address_roles = IpAddressRole.all @servers = Server.all @ip_address_users = IpAddressUser.all end def create @ip_address_roles = IpAddressRole.all @servers = Server.all @ip_address_users = IpAddressUser.all @ip_address = IpAddress.new(ip_address_params) @ip_address.save redirect_to ip_addresses_path #render plain: params[:ip_address].inspect end def destroy @ip_address = IpAddress.find(params[:id]) @ip_address.destroy flash[:success] = "Record successfully deleted" redirect_to ip_addresses_path end private def ip_address_params params.require(:ip_address).permit(:title, :server, :user, :is_in_use, :role) end end
And ip_address.rb code is here:
class IpAddress < ActiveRecord::Base def is_ip_address_in_use if self.is_in_use == 'true' self.is_in_use = 'yes' else self.is_in_use = 'no' end end end
I have some great resources near hand, but my controller is getting fat, and model skinny, also, in my controller I have a lot of repetition. How can I refactor model and controller? Controller is really ugly.