Guest User

Untitled

a guest
Jul 9th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.30 KB | None | 0 0
  1. #models/like.rb
  2. class Like < ActiveRecord::Base
  3.   belongs_to :tweet
  4.   belongs_to :user
  5.   validates :user_id, presence: true
  6.   validates :tweet_id, presence: true
  7. end
  8.  
  9. #models/tweet.rb
  10. class Tweet < ActiveRecord::Base
  11.   belongs_to :user
  12.   validates :text, presence: true, length: { maximum: 140 }
  13. end
  14.  
  15. #models/user.rb
  16. class User < ActiveRecord::Base
  17.   has_many :tweets
  18.   has_many :likes
  19.   validates :email, presence: true, uniqueness: true
  20.   validates :password, presence: true, length: { minimum: 6, maximum: 25 }
  21.   validates :name, presence: true, uniqueness: true, format: { with: /\A[a-zA-Z0-9]+\Z/ }
  22. end
  23.  
  24. #views/tweets/show.html.erb
  25. <p id="notice"><%= notice %></p>
  26.  
  27. <p>
  28.   <strong>Text:</strong>
  29.   <%= @tweet.text %>
  30. </p>
  31.  
  32. <p>
  33.   <strong>User:</strong>
  34.   <%= @tweet.user.name %>
  35. </p>
  36.  
  37. <p>
  38.   <%= link_to 'Like', @tweet.like %>
  39. </p>
  40.  
  41. <%= link_to 'Edit', edit_tweet_path(@tweet) %> |
  42. <%= link_to 'Back', tweets_path %>
  43.  
  44. #controllers/tweets_controller.rb
  45. class TweetsController < ApplicationController
  46.   before_action :set_tweet, only: [:show, :edit, :update, :destroy]
  47.  
  48.   # GET /tweets
  49.   # GET /tweets.json
  50.   def index
  51.     @tweets = Tweet.all
  52.   end
  53.  
  54.   # GET /tweets/1
  55.   # GET /tweets/1.json
  56.   def show
  57.   end
  58.  
  59.   # GET /tweets/new
  60.   def new
  61.     @tweet = Tweet.new
  62.   end
  63.  
  64.   # GET /tweets/1/edit
  65.   def edit
  66.   end
  67.  
  68.   # POST /tweets
  69.   # POST /tweets.json
  70.   def create
  71.     @tweet = Tweet.new(tweet_params)
  72.  
  73.     respond_to do |format|
  74.       if @tweet.save
  75.         format.html { redirect_to @tweet, notice: 'Tweet was successfully created.' }
  76.         format.json { render :show, status: :created, location: @tweet }
  77.       else
  78.         format.html { render :new }
  79.         format.json { render json: @tweet.errors, status: :unprocessable_entity }
  80.       end
  81.     end
  82.   end
  83.  
  84.   # PATCH/PUT /tweets/1
  85.   # PATCH/PUT /tweets/1.json
  86.   def update
  87.     respond_to do |format|
  88.       if @tweet.update(tweet_params)
  89.         format.html { redirect_to @tweet, notice: 'Tweet was successfully updated.' }
  90.         format.json { render :show, status: :ok, location: @tweet }
  91.       else
  92.         format.html { render :edit }
  93.         format.json { render json: @tweet.errors, status: :unprocessable_entity }
  94.       end
  95.     end
  96.   end
  97.  
  98.   # DELETE /tweets/1
  99.   # DELETE /tweets/1.json
  100.   def destroy
  101.     @tweet.destroy
  102.     respond_to do |format|
  103.       format.html { redirect_to tweets_url, notice: 'Tweet was successfully destroyed.' }
  104.       format.json { head :no_content }
  105.     end
  106.   end
  107.  
  108.   def like
  109.     @tweet.likes.new(params[:user_id, :tweet_id])
  110.     respond_to do |format|
  111.       if @tweet.like.save
  112.         format.html { redirect_to @tweet, notice: 'Like succeded.'}
  113.         format.json { render :show, status: :ok, location: @tweet }
  114.       else
  115.         @tweet.like.destroy
  116.         format.html { render :edit, notice: 'You dont like this anymore' }
  117.         format.json { render json: @tweet.like.errors, status: :unprocessable_entity }
  118.       end
  119.     end
  120.   end
  121.  
  122.   private
  123.     # Use callbacks to share common setup or constraints between actions.
  124.     def set_tweet
  125.       @tweet = Tweet.find(params[:id])
  126.     end
  127.  
  128.     # Never trust parameters from the scary internet, only allow the white list through.
  129.     def tweet_params
  130.       params.require(:tweet).permit(:text, :user_id)
  131.     end
  132. end
Advertisement
Add Comment
Please, Sign In to add comment