Rails Controllers Flashcards
controller index (without views)
def index
books = Book.all
render json: books
end
controller show (without views)
def show
book = Book.find_by(id: [params[:id])
render json: book
end
How do you check what routes are available?
bundle exec rake routes
What do I do when I want something to be the VIEW of an index controller action?
app/views/books/index.html.erb
its going to be an html file, and erb is embedded ruby code.
controller index (with views)
def index
@books = Books.all #this allows me to call the @books instance variable. we can do this b/c of erb.
render ‘index’ #this renders a template
end
How do we write ruby code that not be printed in the erb?
shortcut in atom: - tab
How do we write ruby code that will be printed in the erb?
shortcut in atom: = tab
How to show each book in <ul> in erb?</ul>
<ul>
<li>
</ul>
</li></ul>
controller show (with views)
def show
@book = Book.find_by(:id, params[:id]) #because we want to access it in the html.erb
render :show
end
What do I do when I want something to be the VIEW of an SHOW controller action?
show.html.erb
What if we don’t find the book we are trying to show?
def show
@book = Book.find_by(:id, params[:id]) #because we want to access it in the html.erb
if @book
render :show #packages up our response and give back to the cleint
else
render :index
redirect_to ‘/books’ #its in our routes!
#hey, make a new request.
end
end
How many redirects can we have in a method?
One. Redirect does not end execution of your meothd. Might get a double render error.
What does a template missing error mean?
We were able to find this controller action, but we couldn’t find a template.
New in views?
def new
render :new
end
new.html.erb
<h1> Add book to Library! </h1>
form action=”/books” method=”post”>
Title
new.html.erb and user’s info gets into the ??
<h1> Add book to Library! </h1>
form action=”/books” method=”post”>
Title
Example of a params hash-like object?
{“title” => “Harry Potter”, “controller” => “books”, “action”=>”create”}
Convention in terminal for generating a migration?
rails generate migration CreateTableName
When you create a model, is it plural or singular?
User (singular)
Inside a form, how do you create labels?
Title
Author
Year
in schema,
t.string “category”
Lets us choose stuff easier
We want of what to be inside a key of book?
our params! {“title” => “Harry Potter”, “controller” => “books”, “action”=>”create”}
we don’t have to fish out, we can just say params[:book] in html.
How do we nest our params variable inside our label html tags?
Title
Author
Year
How do we add category, drop down?
Fiction!
How do we add category, drop down without a default?
–Please select–
Fiction!
Fiction!
How do we give them more space to write a description?
Description
How can we add break lines in our new.html.erb?
<br></br><br></br>
How do we add a label?
Description
How do i get a nice calendar drop down?
instead of:
How do we get our data to persist?
We need an action and a method.
We nested the params data under the key of book name=”book[title]” (through the name attributeo n the html page itself)
this lets our data persist!
How do we CREATE an instance of a book?
@book (instance variables are accessible in the views.)
What do we want to pass into our new instance of book?
params!
def create
@book = Book.new(params[:book])
end
If we can’t save the new book, where do we want to send the user?
back to the form.
How do we send the user back to the form in case of an error?
def create @book = Book.new(params[:book]) if @book.save redirect_to book_url(@book) #it extracts the id for us else render :new end
What if our user added new fields to our params?
How do we use strong params?
To only allow the user to only use the fields we want?
def create @book = Book.new(book_params)
##if @book.save, redirect_to book_url(@book) else render:new end## end
private
def book_params (convention is to take name of the model and append it to params)
params.require(:book).permit(:title, :author, :year, :description, :category)
What happens if we try to pass our book directly into Book.new ?
rails throws an error.
protect_from_forgery with: exception
this is commented out just now only because we haven’t learned auth.
What happens if you go to localhost:3000/book/new?
its not
localhost:3000/books/new
so you get a routing error.
What should you think if you get a routing error?
- What route was I trying to go to?
- What route did I go to?
- Why did I go to this route?
How do I begin to see
1. What route was I trying to go to?
go to bundle exec rake routes
and see what routes are available.
Now run the rake routes table.
If you don’t know where to go, go to…
The rails server logs. that’s where we see what rails tried to do with that information.
What is Book Load in server logs?
Our SQL query! if its returning the wrong information, then we can see what’s actually being run in SQL.
What’s are two good gems to debug our controller?
gem ‘better_errors’ #better stack trace
gem ‘binding_of_caller’ #opens an interactive repl before our code crashes. WOW! sandbox environment.
Don’t forget to ____ after you bundle install better_errors and binding_of_caller)
restart the console!
rails c
What keyword do we need to use to hit our stack trace?
We want to use keyword
fail
to get binding_of_caller and better_errors to work.
@book.errors.full_messages
[“Year cannot be in the future”]
These error messages come from the model.
Whats going wrong in our model?
We can use byebug! (But DON’T require byebug at the top, its default installed in your gemfile.)
If you don’t c
out of byebug, what will happen?
You will not be able to make new requests, you will be hung in this byebug.
When I see COMMIT, what do I know?
I know the data persisted to the database.
302?
redirect to the show page.
Where do we put gem
‘better_errors’
gem ‘binding_of_caller’
in the gemfile
?
in the group: development!
that way they will only be used in production.
we don’t want hackers to see our database!