Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #models/like.rb
- class Like < ActiveRecord::Base
- belongs_to :tweet
- belongs_to :user
- validates :user_id, presence: true
- validates :tweet_id, presence: true
- end
- #models/tweet.rb
- class Tweet < ActiveRecord::Base
- belongs_to :user
- validates :text, presence: true, length: { maximum: 140 }
- end
- #models/user.rb
- class User < ActiveRecord::Base
- has_many :tweets
- has_many :likes
- validates :email, presence: true, uniqueness: true
- validates :password, presence: true, length: { minimum: 6, maximum: 25 }
- validates :name, presence: true, uniqueness: true, format: { with: /\A[a-zA-Z0-9]+\Z/ }
- end
- #views/tweets/show.html.erb
- <p id="notice"><%= notice %></p>
- <p>
- <strong>Text:</strong>
- <%= @tweet.text %>
- </p>
- <p>
- <strong>User:</strong>
- <%= @tweet.user.name %>
- </p>
- <p>
- <%= link_to 'Like', @tweet.like %>
- </p>
- <%= link_to 'Edit', edit_tweet_path(@tweet) %> |
- <%= link_to 'Back', tweets_path %>
- #controllers/tweets_controller.rb
- class TweetsController < ApplicationController
- before_action :set_tweet, only: [:show, :edit, :update, :destroy]
- # GET /tweets
- # GET /tweets.json
- def index
- @tweets = Tweet.all
- end
- # GET /tweets/1
- # GET /tweets/1.json
- def show
- end
- # GET /tweets/new
- def new
- @tweet = Tweet.new
- end
- # GET /tweets/1/edit
- def edit
- end
- # POST /tweets
- # POST /tweets.json
- def create
- @tweet = Tweet.new(tweet_params)
- respond_to do |format|
- if @tweet.save
- format.html { redirect_to @tweet, notice: 'Tweet was successfully created.' }
- format.json { render :show, status: :created, location: @tweet }
- else
- format.html { render :new }
- format.json { render json: @tweet.errors, status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /tweets/1
- # PATCH/PUT /tweets/1.json
- def update
- respond_to do |format|
- if @tweet.update(tweet_params)
- format.html { redirect_to @tweet, notice: 'Tweet was successfully updated.' }
- format.json { render :show, status: :ok, location: @tweet }
- else
- format.html { render :edit }
- format.json { render json: @tweet.errors, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /tweets/1
- # DELETE /tweets/1.json
- def destroy
- @tweet.destroy
- respond_to do |format|
- format.html { redirect_to tweets_url, notice: 'Tweet was successfully destroyed.' }
- format.json { head :no_content }
- end
- end
- def like
- @tweet.likes.new(params[:user_id, :tweet_id])
- respond_to do |format|
- if @tweet.like.save
- format.html { redirect_to @tweet, notice: 'Like succeded.'}
- format.json { render :show, status: :ok, location: @tweet }
- else
- @tweet.like.destroy
- format.html { render :edit, notice: 'You dont like this anymore' }
- format.json { render json: @tweet.like.errors, status: :unprocessable_entity }
- end
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_tweet
- @tweet = Tweet.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def tweet_params
- params.require(:tweet).permit(:text, :user_id)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment