Skip to content

Instantly share code, notes, and snippets.

@rickyhaswifi
Last active August 30, 2019 00:51
Show Gist options
  • Save rickyhaswifi/b21f2fcaabc3be748ee8bb84171f2071 to your computer and use it in GitHub Desktop.
Save rickyhaswifi/b21f2fcaabc3be748ee8bb84171f2071 to your computer and use it in GitHub Desktop.
The patter for rails controller
# READ - GET
# Page will see and interact with
# shows record from the db onto the pages
# index action - see all of the record for the table
@model_names = Model_name.all
# gets all the records and set them to a variable
# show action - see a single record, id
@model_name = Model_name.find(params[:id])
# new action - display the new record form, creates the
# record in memory
@model_name = Model_name.new
# edit action - displays the edit record form, id
@model_name = Model_name.find(params[:id])
# Create / POST
# - create action create a record, from the new action
@model_name = Model_name.new(model_name_params)
if @model_name.save
do something
else
render :new
end
# UPDATE / PUT PATCH
# - update action, edit a record, edit form , id
@model_name = Model_name.find(params[:id])
if @model_name.update(model_name_params)
do something
else
render :edit
end
# Destroy / DELETE
# - destroy action delete a record, id
Model_name.find(params[:id]).destroy
do something
# model_name_params
# - pass certain attr
private
def model_name_params
params.require(:model_name).permit(:attr, :everything :that :table :has)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment