Rails Controllers Flashcards

1
Q

controller index (without views)

A

def index
books = Book.all
render json: books
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

controller show (without views)

A

def show
book = Book.find_by(id: [params[:id])
render json: book
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you check what routes are available?

A

bundle exec rake routes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What do I do when I want something to be the VIEW of an index controller action?

A

app/views/books/index.html.erb

its going to be an html file, and erb is embedded ruby code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

controller index (with views)

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do we write ruby code that not be printed in the erb?

A

shortcut in atom: - tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we write ruby code that will be printed in the erb?

A

shortcut in atom: = tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to show each book in <ul> in erb?</ul>

A

<ul>

<li>

</ul>
</li></ul>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

controller show (with views)

A

def show
@book = Book.find_by(:id, params[:id]) #because we want to access it in the html.erb
render :show
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do I do when I want something to be the VIEW of an SHOW controller action?

A

show.html.erb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What if we don’t find the book we are trying to show?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How many redirects can we have in a method?

A

One. Redirect does not end execution of your meothd. Might get a double render error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does a template missing error mean?

A

We were able to find this controller action, but we couldn’t find a template.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

New in views?

A

def new
render :new
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

new.html.erb

A

<h1> Add book to Library! </h1>

form action=”/books” method=”post”>
Title

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

new.html.erb and user’s info gets into the ??

A

<h1> Add book to Library! </h1>

form action=”/books” method=”post”>
Title

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Example of a params hash-like object?

A

{“title” => “Harry Potter”, “controller” => “books”, “action”=>”create”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Convention in terminal for generating a migration?

A

rails generate migration CreateTableName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

When you create a model, is it plural or singular?

A

User (singular)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Inside a form, how do you create labels?

A

Title

Author

Year

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

in schema,

t.string “category”

A

Lets us choose stuff easier

22
Q

We want of what to be inside a key of book?

A

our params! {“title” => “Harry Potter”, “controller” => “books”, “action”=>”create”}

we don’t have to fish out, we can just say params[:book] in html.

23
Q

How do we nest our params variable inside our label html tags?

A

Title

Author

Year

24
Q

How do we add category, drop down?

A

Fiction!

25
Q

How do we add category, drop down without a default?

A

–Please select–
Fiction!
Fiction!

26
Q

How do we give them more space to write a description?

A

Description

27
Q

How can we add break lines in our new.html.erb?

A

<br></br><br></br>

28
Q

How do we add a label?

A

Description

29
Q

How do i get a nice calendar drop down?

A

instead of:

30
Q

How do we get our data to persist?

A

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!

31
Q

How do we CREATE an instance of a book?

A

@book (instance variables are accessible in the views.)

32
Q

What do we want to pass into our new instance of book?

A

params!
def create
@book = Book.new(params[:book])
end

33
Q

If we can’t save the new book, where do we want to send the user?

A

back to the form.

34
Q

How do we send the user back to the form in case of an error?

A
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
35
Q

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?

A
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)

36
Q

What happens if we try to pass our book directly into Book.new ?

A

rails throws an error.

37
Q

protect_from_forgery with: exception

A

this is commented out just now only because we haven’t learned auth.

38
Q

What happens if you go to localhost:3000/book/new?

A

its not
localhost:3000/books/new

so you get a routing error.

39
Q

What should you think if you get a routing error?

A
  1. What route was I trying to go to?
  2. What route did I go to?
  3. Why did I go to this route?
40
Q

How do I begin to see

1. What route was I trying to go to?

A

go to bundle exec rake routes

and see what routes are available.

Now run the rake routes table.

41
Q

If you don’t know where to go, go to…

A

The rails server logs. that’s where we see what rails tried to do with that information.

42
Q

What is Book Load in server logs?

A

Our SQL query! if its returning the wrong information, then we can see what’s actually being run in SQL.

43
Q

What’s are two good gems to debug our controller?

A

gem ‘better_errors’ #better stack trace

gem ‘binding_of_caller’ #opens an interactive repl before our code crashes. WOW! sandbox environment.

44
Q

Don’t forget to ____ after you bundle install better_errors and binding_of_caller)

A

restart the console!

rails c

45
Q

What keyword do we need to use to hit our stack trace?

A

We want to use keyword

fail

to get binding_of_caller and better_errors to work.

46
Q

@book.errors.full_messages

[“Year cannot be in the future”]

A

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.)

47
Q

If you don’t c

out of byebug, what will happen?

A

You will not be able to make new requests, you will be hung in this byebug.

48
Q

When I see COMMIT, what do I know?

A

I know the data persisted to the database.

49
Q

302?

A

redirect to the show page.

50
Q

Where do we put gem
‘better_errors’
gem ‘binding_of_caller’

in the gemfile
?

A

in the group: development!

that way they will only be used in production.
we don’t want hackers to see our database!