-
-
Save amysimmons/5b71eedf86c36398416e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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