Rails views Flashcards

1
Q

How to create a link with post request?

A
# post 'custom', controller: 'articles', action: 'custom'
button_to 'Delete', '/hello', method: :delete
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between form_tag, form_for and form_with?

A

form_for and form_tag will be deprecated and replaced with form_with.

  • form_for for a class
  • form_tag without a class
  • form_with can cover both cases
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to create a form without model?

A

= form_with url: ‘/hello’, method: :post do |form|
= form.text_field :name
= form.submit

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

How does url_for helper works?

A
# this method takes 2 argument and generate url
url_for(action: 'hello_new', controller: 'application' )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to add redirect back?

A

redirect_back fallback_location: ‘/’

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

How to create a button with a delete request?

A

button_to ‘Delete’, ‘/hello’, method: :delete

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

How to use content_for?

A

A part of the code which we can reuse
= content_for :left do
h1 Hello left
h2 hello

= content_for :right do
h1 Hello right
h2 hello right

# you can run this content by 2 ways
= yield :left
= yield :right

= content_for :right
= content_for :left

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

How you can wrap a ruby code with a tag?

A

= content_tag(‘h1’, 1 + 1) # 2
# another way
h1
= 1 + 1

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

How to render partial from the view?

A

app/views/animals/_partial.html.erb
= render ‘partial’
# app/views/my_examples/_partial.html.erb
= render ‘my_example/partial’

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

How to add check_box to the view form?

A

= form.check_box :alive

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

How to add password_field to form?

A

= form.password_field :password

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

How to send variables via partial?

A

= render ‘partial’, hello: 10, sample: ‘Sample’

inside the partial
= hello
= sample

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

How to add text area field?

A

= form.text_area :area

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

How to check all local variables inside the partial?

A
= render 'partial', hello: 10, sample: 'Sample'
# inside the partial
= local_assigns
{:hello=>10, :sample=>"Sample"}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add image tag?

A
# app/assets/images/sample.png
= image_tag 'sample.png'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to get a path/url to the image?

A

= image_path ‘sample.png’

= image_url ‘sample.png’

17
Q

How to add date/time field>

A

= form.date_field :area

= form.time_field :area

18
Q

What url and request type use “form_with model: Cat.last”?

A

url => /cat/:id

method => patch

19
Q

What url and request type use “form_with model: Cat.new”?

A

url => /cats

method => post

20
Q

How to show params in a controller for the form_with model: Cat.last?

A

params[:cat][:name]

21
Q

How to change default name from the params?

A
# use scope
= form_with model: Cat.new, scope: :dog do |form|
  = form.text_field :name

= form.submit

# controller 
params[:dog][:name]
22
Q

How to add a hidden fields to form in rails?

A

= form.hidden_field :cool

23
Q

How to add a label to the form?

A

= form.label :name

24
Q

How to add a class to the field?

A

= form.text_area :area, class: ‘materialize-textarea’

25
Q

How to add many other tags inside link_to ?

A

= link_to cards_path, class: “waves-effect waves-light btn” do
<i>bookmarks</i>
Cards

26
Q

How to click the button and send params?

A

button_to ‘1’, some_path, method: :patch, params: { confidence_level: 1 }