Skip to content

Instantly share code, notes, and snippets.

@dohoonk
Created February 5, 2016 04:49
Show Gist options
  • Select an option

  • Save dohoonk/c65f0edbd0de6a32a499 to your computer and use it in GitHub Desktop.

Select an option

Save dohoonk/c65f0edbd0de6a32a499 to your computer and use it in GitHub Desktop.
codcore

#TDD

refactoring code

before_action :find_question, only:[:show, :edit, :update, :destroy] before_action :find_question, except:[:new, :create, index]

this will call find_question action before all the other action

def before_action
  @question = Question.find params[:id]
end

Now we can delete al the @question = Question.find params[:id] lines

How to remove the duplicates in html.erb files

We can define part of erb file as a partial by using underline infront of the file name

_form.html.erb

after copying the comman code inside of other erb files place it on _form.html.erb file

afterwards replace the comman code with this

<%= render "form" %>

you can even pass an local variable in a hash

<%= render "form", my_var: my_variable %>

to display a message to the user for a short time

flash[:notice] = "Question Created Successfully"

afterwards write the code <%= notice %>

in the view, layout page

flash[:alert] = "Question was not created. Check errors below

<%= notice || alert %>

for redirect_ to

notice: "Question updated!"

notice: "Question deleted!"

RSpec

Given..When..Then..

Given --------> no question in DB existing question in DB

When ----------> create question with same title

Then ----------> no question created in the DB

Ruby motion

rspec-rails

rails new fundsy -T -d postgresql

bin/rails generate rspec:install

bin/rake db:create RAILS_ENV=test

bin/rails g

bin/rails g rspec:model campaign

bin/rails g model campaign name description:text goal:integer end_date:datetime

for delete

bin/rails d model campaign name description:text goal:integer end_date:datetime

in your model

validates :name, presence: true

rspec spec/controllers/campaigns_controller_spec.rb:5

resources :campaigns, only: [:new]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment