Week 2 Questions Flashcards
How would you generate a mailer and it’s email templates?
rails generate mailer UserMailer account_activation password_reset
When you generate a mailer in the command line it creates two views for the email template. Why does this happend
One for plain-text email and one for HTML email.
What does user:references add in this migration:
rails generate model Micropost content:text user:references
Automatically adds a user_id column (along with an index and a foreign key reference)3 for use in the user/micropost association.
How do you set a default scope for an object to be in descending order
#in the model default_scope -> { order(created_at: :desc) }
What validation would you write to destroy a post that belongs to a user that has been destroyed
#in user model has_many :microposts, dependent: :destroy
How would you code a shared errors partial and how would you code the render snippet?
<div> <div class="alert alert-danger"> The form contains . </div> <ul>
<li> </ul>
</div>
Explain this code:
Micropost.where(“user_id = ?”, id)
Find microposts where the user_id is equal to the id of the current user
How would you use the pluralize method on microposts belonging to a user
What does the request.referrer method do?
It goes to the previous URL
In one line, how would you write an image tag that displays a micropost’s image if it has one
In the ruby language, what are the differences between Array.length, Array.size, and Array.count?
Array#length method used on arrays in ruby returns the number of elements in the array
Array#size is just an alias to the Array#length method. It executes same Array#length method internally.
Array#count has some more functionalities than length/size. It can be used for getting number of elements based on some condition.
3 ways to use Array#count:
c = [1,2,3,4,4,7,7,7,9]
1. Array#count Returns number of elements in Array
c.count
=> 9
2. Array#count n Returns number of elements having value n in Array
c.count 7
=> 3
3. Array#count{|i| i.even?} Returns count based on condition invoked on each element array
c.count {|i| i>5}
=> 4
How would you write an if else statement depending on a specific controller action?
if params[:action] == "foo" # Show stuff for action 'foo' elsif params[:action] == "bar" # Show stuff for action 'bar' elsif ... # etc. end
How do you apply a layout in the controller?
#create a new layout_name.hmtl.erb file under layouts in views #in the controller layout 'layout_name'
How do you view all the rails generator commands in the terminal?
rails g
What is required at the top of each spec?
require ‘rails_helper’
Write a simple feature spec for visiting the homepage and seeing a h1 tag with jar of awesome as the text
require 'rails_helper' feature "user visits homepage" do scenario "successful visit" do visit root_path expect(page).to have_css 'h1', text: 'Jar of Awesome' end end
How would you write an acceptance test for the creation of a note?
require 'rails_helper' feature "User create note" do scenario "successful" do visit root_path click_on "New Note" fill_in "Conent", with: "Ate pad thai with sweet tangerines" click_on "Submit"
expect(page).to have_css '.notes', text: 'Ate pad thai with sweet tangerines' end end
Where do you place Object Pattern
spec/support/object_pattern.rb
How do you include Capybara methods in a Object Pattern file?
include Capybara::DSL in the class
class NewNoteForm
include Capybara::DSL
end
When writing methods for Object Pattern, why would you return self in this method? def visit_page visit('/') click_on "New Note" self end
You return self so that you can chain multiple methods to the object.
What do you have to add to the top of the spec to use and Object Pattern object?
require_relative ‘../support/new_note_form’
How do you code a let statement for a new_note_form
let(:new_note_form){NewNoteForm.new}
How would you write a scenario test using Factory Girl that visits the show page of a particular note object that has content ‘Just did it’?
scenario ‘note page’ do
note = FactoryGirl.create(:note, content: ‘Just did it’)
visit(‘/notes/#{note.id}’)
expect(page).to have_content('Just did it') end
fix this–>How would you write the enum for a privacy option for a note class in the note.rb file? How would you write the input for a privacy option using simple form?
simple form in views
#note.rb enum privacy: [:public_note, :private_note, :friends_access]