Skip to content

Instantly share code, notes, and snippets.

@lengarvey
Last active August 29, 2015 14:18
Show Gist options
  • Save lengarvey/39d4cb8927ab0533fda8 to your computer and use it in GitHub Desktop.
Save lengarvey/39d4cb8927ab0533fda8 to your computer and use it in GitHub Desktop.
class FooController
def index
# The controller is responsible for gathering the things needed
# to make the response. This should be a single line of code
@things = Foo.things
# The controller is responsible for sending the view back to the user.
# the view uses our instance variable above to render @things
render :some_view
end
end
# in an ideal controller those are the only two things that the action does
# 1. gathers the data
# 2. renders the view
#
# ideally each of these things is 1 line of code, but it might end up being
# more to deal with validation errors etc
class Foo
# the model is responsible for databasey type things
# relationships are part of that
belongs_to :bar
has_many :widgets
# so are validations
validates_presence_of :some_field
# so are scopes
scope :active, -> { where(state: 'active') }
# often people put extra functionality into the model
def activate!
update(state: 'active')
send_email_to_admin
end
# this is good practice when starting out, but your models
# can become "fat" with this extra functionality.
# do this at first but you'll learn more about this later
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment