Last active
February 19, 2016 17:07
-
-
Save dohoonk/ab03ea0b63d23cf42407 to your computer and use it in GitHub Desktop.
codecore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #TDD | |
| association :user, Factorygirl: :user | |
| bin/rails g migration add_user_references_to_campaigns user:references | |
| RAILS_ENV=test | |
| let(:pledge) {FactoryGirl.create(:pledge, {campaign: campaign, user: user})} | |
| call pledge | |
| pledge | |
| redirect_to campaign | |
| for raising an error message in rspec | |
| expect do | |
| delete :destroy, id: pledge_1.id, campaign_id: pledge_1.campaign_id | |
| end.to raise_error | |
| pledge = current_user.pledges.find_params[:id] | |
| -question.rb | |
| has_many :users, through: :likes | |
| -user.rb | |
| has_many :liked_questions, through: :likes, source: :questions | |
| like.create(question: q, user: u) | |
| user.liked_question << question | |
| qustion.user << user | |
| u = user.find(3) | |
| user.liked_questions | |
| user.liked_questions_ids | |
| user.liked_questions_ids = [101, 102] | |
| -------------------------------------------- | |
| bin/rails g controller likes | |
| -routes.rb | |
| resoures :likes, only: [:create, :destroy] | |
| -app likes controller | |
| before_action :authenticate_user | |
| def create | |
| q = Question.find params[:question_id] | |
| current_user.liked_questions << q | |
| redirect_to q | |
| end | |
| def destroy | |
| end | |
| - questions show | |
| <hr> | |
| <%= link_to "Link", question_likes_path(@qustion), method: :post %> | |
| <hr> | |
| - like.model | |
| this ensures that user_id /question_id combination is unique | |
| validates :user_id, uniqueness: {scope: :question_id} | |
| -likes controller | |
| def create | |
| q = Question.find params[:question_id] | |
| like = Like.new(questino: q, user: current_user) | |
| if like.save | |
| redirect_to q, notice: "Question liked" | |
| else | |
| redirect_to q, alert: "Not liked!" | |
| end | |
| end | |
| -show.html | |
| <% like [email protected]_by_user_id current_user %> | |
| <%= link_to "unlike", question_like_path(@question, like), method: :delete %> | |
| -like controller | |
| def destroy | |
| like = like.find params[:id] | |
| q = Question.find params[:question_id] | |
| like.destroy | |
| redirect_to question_path(q), notice: "Un-liked" | |
| end | |
| -show.html | |
| <% if like %> | |
| ----------------------------------------------------------------------------------------- | |
| ## refactoring | |
| -question.rb | |
| def like_for(user) | |
| likes.find_by_user_id user | |
| end | |
| -show.html | |
| <%= @question.likes.count %> | |
| ------------------------------------------------------------------------------------- | |
| font awesome rails | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment