Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ = jQuery
- $(document).on "ready page:load", ->
- $('#follow_btn').on "click", ->
- friend = $(this).data("friend")
- boton = $(this)
- $.ajax "/usuario/follow",
- type: "POST"
- dataType: "JSON"
- data: {usuario: { friend_id: friend }}
- success: (data)->
- console.log data
- boton.slideUp()
- alert friend
- error: (err)->
- console.log err
- alert "No hemos podido crear la amistad"
- class UsuarioController < ApplicationController
- skip_before_action :verify_authenticity_token
- def show
- @usuario = Usuario.find(params[:id])
- end
- def follow
- respond_to do |format|
- if current_usuario.follow!(post_params)
- format.json {head :no_content}
- else
- format.json {render json: "Se encontraron errores"}
- end
- end
- end
- private
- def post_params
- params.require(:usuario).permit(:friend_id)
- end
- end
- def follow!(amigo_id)
- friendships.create(friend_id = amigo_id)
- end
- current_usuario.follow!(3)
- class Usuario < ApplicationRecord
- # Include default devise modules. Others available are:
- # :confirmable, :lockable, :timeoutable and :omniauthable
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable
- devise :omniauthable, omniauth_providers: [:facebook, :twitter]
- has_many :posts
- has_many :friendships
- has_many :follows, through: :friendships, source: :friend
- has_many :followers_friendships, class_name: "Friendship",
- foreign_key: "friend_id"
- has_many :followers, through: :followers_friendships, source:
- :usuario
- def follow!(amigo_id)
- friendships.create!(friend_id: amigo_id)
- end
- def can_follow?(amigo_id)
- not amigo_id == self.id or friendships.where(friend_id:
- amigo_id).size > 0
- end
- def email_required?
- false
- end
- validates :username, presence: true, uniqueness: true,
- length: {in:5..20, too_short: "Al menos 5 caracteres", too_long:
- "Maximo 20 caracteres"}
- def self.find_or_create_by_omniauth(auth)
- usuario = self.find_or_create_by(provider: auth[:provider], uid:
- auth[:uid]) do |user|
- user.nombre = auth[:name]
- user.apellido = auth[:last_name]
- user.username = auth[:username]
- user.email = auth[:email]
- user.uid = auth[:uid]
- user.provider = auth[:provider]
- user.password = Devise.friendly_token[0,20]
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement