Last active
February 21, 2016 01:31
-
-
Save dohoonk/eee45bb927f6771d4831 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
| # | |
| gem rails-erd | |
| brew install graphviz | |
| bin/rake erd | |
| -------------------------------- | |
| -questions.rb | |
| has_many :taggings, dependent: :destroy | |
| has_many :tags, through: :taggings | |
| -tag.rb | |
| has_many :taggings, dependent: :destroy | |
| has_many :questions, throught: :taggings | |
| -tagging | |
| validates :tag_id, :question_id, presence: true | |
| validates :tag_id, uniqueness: {scope: :question_id} | |
| -seed | |
| ["Coffee", "Snow", "Hockey", "Bow Buns", "Money"].each do |cat| | |
| Category.create(name: cat) | |
| end | |
| -tag.rb | |
| validates :name, presence: true, uniqueness: true | |
| -run your seed | |
| bin/rake db:seed | |
| q.tags_id = [2,4] | |
| collection check box rails | |
| _form.html.erb | |
| <div> | |
| <%= f.collection_check_boxes(:tag_ids, Tag.order("name"), :id, :name) %> | |
| </div> | |
| -------------------------------------------------- | |
| bin/rails g model vote user: references question: references vote_up: boolean | |
| -user.rb | |
| has_many :voted_questions, through: :votes, source: :question | |
| -question.rb | |
| has_many :voiting_users, through: :votes, source: :user | |
| -vote.rb | |
| validates :question_id, uniqueness: {scope: :user_id} | |
| -generate controller | |
| bin/rails g controller votes | |
| -routes.rb | |
| resources :votes, only: [:create,:update,:destroy] | |
| -votes controller | |
| before_action :authenticate_user | |
| def create | |
| question = Question.find params[:question_id] | |
| vote = Vote.new vote_params | |
| vote.user = current_user | |
| vote.question = question | |
| flash = (vote.save) ? {notice: "Voted!"} : {alert: "Try again!"} | |
| ##or | |
| if vote.save | |
| redirect_to question_path(question),notice: "Voted!" | |
| else | |
| redirect_to question_path(question), alert: "Try again!" | |
| end | |
| end | |
| def update | |
| end | |
| def destroy | |
| end | |
| private | |
| def vote_params | |
| params.require(:vote).permit(:is_up) | |
| end | |
| end | |
| -question.show | |
| <%= link_to "Vote Up", question_votes_path(@question, {vote: {is_up: true}}), | |
| method: :post%> | |
| <%= link_to "Vote Down", question_votes_path(@question, {vote: {is_up: false}}), | |
| method: :post%> | |
| -question.rb | |
| def vote_for(user) | |
| votes.find_by_user_id user | |
| end | |
| -question.show | |
| <% vote = @question.vote_for(current_user) %> | |
| <%if !vote%> | |
| <%= link_to "Vote Up", question_votes_path(@question, {vote: {is_up: true}}), | |
| method: :post%> | |
| <%= link_to "Vote Down", question_votes_path(@question, {vote: {is_up: false}}), | |
| method: :post%> | |
| <% elsif vote.is_up? %> | |
| <% link_to "Remove Vote up", question_vote_path(@question, vote), method: :delete %> | |
| <% else %> | |
| <% end %> | |
| -votes controller | |
| def destroy | |
| question = Question.find params[:question_id] | |
| vote = Vote.find params[:id] | |
| vote.destroy | |
| redirect_to question_path(question), notice: "Vote removed" | |
| end | |
| -question.show | |
| <% vote = @question.vote_for(current_user) %> | |
| <%if !vote%> | |
| <%= link_to "Vote Up", question_votes_path(@question, {vote: {is_up: true}}), | |
| method: :post%> | |
| <%= link_to "Vote Down", question_votes_path(@question, {vote: {is_up: false}}), | |
| method: :post%> | |
| <% elsif vote.is_up? %> | |
| <% link_to "Remove Vote up", question_vote_path(@question, vote), method: :delete %> | |
| <% else %> | |
| <% link_to "Remove Vote down", question_vote_path(@question, vote), method: :delete %> | |
| <% end %> | |
| -question.show | |
| <% vote = @question.vote_for(current_user) %> | |
| <%if !vote%> | |
| <%= link_to "Vote Up", question_votes_path(@question, {vote: {is_up: true}}), | |
| method: :post%> | |
| <%= link_to "Vote Down", question_votes_path(@question, {vote: {is_up: false}}), | |
| method: :post%> | |
| <% elsif vote.is_up? %> | |
| <% link_to "Remove Vote up", question_vote_path(@question, vote), method: :delete %> | |
| <%= link_to "Vote Down", question_votes_path(@question, {vote: {is_up: false}}), method: :patch%> | |
| <% else %> | |
| <% link_to "Remove Vote down", question_vote_path(@question, vote), method: :delete %> | |
| <%= link_to "Vote Up", question_votes_path(@question, {vote: {is_up: true}}), method: :patch%> | |
| <% end %> | |
| -vote controller | |
| def update | |
| question = Question.find params[:question_id] | |
| vote = Vote.find params[:id] | |
| if vote.update vote_params | |
| redirect_to question_path(question), notice: "Vote Updated!" | |
| else | |
| redirect_to question_path(question), notice: "try again" | |
| end | |
| end | |
| checking to see if the user is | |
| -vote controller | |
| def update | |
| question = Question.find params[:question_id] | |
| vote = current_user.votes.find params[:id] | |
| if vote.update vote_params | |
| redirect_to question_path(question), notice: "Vote Updated!" | |
| else | |
| redirect_to question_path(question), notice: "try again" | |
| end | |
| end | |
| ----------------------------------------------------------------------- | |
| -question.rb | |
| def vote_result | |
| votes.where(is_up: true).count - votes.where(is_up: false).count | |
| end | |
| def self.up_count | |
| where(is_up: true).count | |
| end | |
| def self.down_count | |
| where(is_up: false).count | |
| end | |
| q.where(is_up: true) | |
| q.where(is_up: false) | |
| scope :up_count,lambda { where(is_up: true).count } | |
| ------------------------------------------------------------------------------------------------------------------------- | |
| # Mailers | |
| -.gitignore | |
| /config/initializers/app_keys.rb | |
| -app_keys.rb | |
| ENV["email_user_name"] = "answerawesome" | |
| ENV["email_password"] = "Sup3r$ecret" | |
| bin/rails g mailer answers_mailer | |
| -answers_mailer.rb | |
| ------------------------------------------------------------------------------ | |
| delay job | |
| gem "delayed_job_active_record" | |
| bundle | |
| rails generate delayed_job:active_record | |
| rake db:migrate | |
| -appllication.rb | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment