Rails views Flashcards
How to create a link with post request?
# post 'custom', controller: 'articles', action: 'custom' button_to 'Delete', '/hello', method: :delete
What is the difference between form_tag, form_for and form_with?
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 to create a form without model?
= form_with url: ‘/hello’, method: :post do |form|
= form.text_field :name
= form.submit
How does url_for helper works?
# this method takes 2 argument and generate url url_for(action: 'hello_new', controller: 'application' )
How to add redirect back?
redirect_back fallback_location: ‘/’
How to create a button with a delete request?
button_to ‘Delete’, ‘/hello’, method: :delete
How to use content_for?
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 you can wrap a ruby code with a tag?
= content_tag(‘h1’, 1 + 1) # 2
# another way
h1
= 1 + 1
How to render partial from the view?
app/views/animals/_partial.html.erb
= render ‘partial’
# app/views/my_examples/_partial.html.erb
= render ‘my_example/partial’
How to add check_box to the view form?
= form.check_box :alive
How to add password_field to form?
= form.password_field :password
How to send variables via partial?
= render ‘partial’, hello: 10, sample: ‘Sample’
inside the partial
= hello
= sample
How to add text area field?
= form.text_area :area
How to check all local variables inside the partial?
= render 'partial', hello: 10, sample: 'Sample' # inside the partial = local_assigns {:hello=>10, :sample=>"Sample"}
How to add image tag?
# app/assets/images/sample.png = image_tag 'sample.png'
How to get a path/url to the image?
= image_path ‘sample.png’
= image_url ‘sample.png’
How to add date/time field>
= form.date_field :area
= form.time_field :area
What url and request type use “form_with model: Cat.last”?
url => /cat/:id
method => patch
What url and request type use “form_with model: Cat.new”?
url => /cats
method => post
How to show params in a controller for the form_with model: Cat.last?
params[:cat][:name]
How to change default name from the params?
# use scope = form_with model: Cat.new, scope: :dog do |form| = form.text_field :name
= form.submit
# controller params[:dog][:name]
How to add a hidden fields to form in rails?
= form.hidden_field :cool
How to add a label to the form?
= form.label :name
How to add a class to the field?
= form.text_area :area, class: ‘materialize-textarea’
How to add many other tags inside link_to ?
= link_to cards_path, class: “waves-effect waves-light btn” do
<i>bookmarks</i>
Cards
How to click the button and send params?
button_to ‘1’, some_path, method: :patch, params: { confidence_level: 1 }