【发布时间】:2018-05-30 23:32:10
【问题描述】:
我正在创建一个社交网络,但在添加“好友请求”时遇到了问题。 当我按下“添加朋友”按钮时,Rails 控制台会显示以下内容。
移民友谊:
class CreateFriendships < ActiveRecord::Migration[5.1]
def change
create_table :friendships do |t|
t.references :user, foreign_key: true
t.references :friend, foreign_key: true
t.string :status
t.timestamps
end
end
end
控制器:
class FriendshipsController < ApplicationController
before_action :find_friend,except: [:index,:update]
before_action :find_friendship, only: [:update]
def create
friendship = Friendship.new(user: current_user, friend: @friend)
respond_to do |format|
if friendship.save
format.html { redirect_to @friend }
format.js
else
format.html {redirect_to @friend, notice: "Error!"}
format.js
end
end
end
private
def find_friend
@friend = User.find(params[:friend_id])
end
end
模特友谊:
class Friendship < ApplicationRecord
include AASM
belongs_to :user
belongs_to :friend, class_name: "User"
validates :user_id, uniqueness:{scope: :friend_id}
aasm column: "status" do
state :pending, initial: true
state :active
state :denied
state :blocked
event :accepted do
transitions from: [:pending], to: [:active]
end
event :rejected do
transitions from: [:pending, :active], to: [:denied]
end
event :eliminated do
transitions from: [:pending, :active, :denied], to: [:blocked]
end
end
end
模型用户:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
validates :rut, presence: true, uniqueness: true, rut: true
validates :username, presence: true
validates :name, presence: true
validates :email, presence: true
validate :validate_username_regex
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable,
:omniauth_providers => [:facebook]
has_many :posts
has_many :friendships
...
def self.from_omniauth(auth)
where(provider: auth["provider"], uid: auth["uid"]).first_or_create do |user|
if auth[:info]
user.email = auth[:info][:email]
user.name = auth[:info][:name]
end
user.password = Devise.friendly_token[0,20]
end
end
...
private
def validate_username_regex
unless username =~ /\A[a-zA-Z]*[a-zA-Z][a-zA-Z0-9_]*\z/
errors.add(:username,"XYZ...")
errors.add(:username,"XXXYYYZZZ...")
end
end
end
添加好友按钮:
=grid xs:4,sm:2 do
=button_to friendships_path(friend_id: @user.id),
method: :post, remote: true, :"data-type" => "script",
class:"mdl-button mdl-js-button mdl-button--fab
mdl-js-ripple-effect" do
%i.material-icons person_add
架构:
create_table "friendships", force: :cascade do |t|
t.integer "user_id"
t.integer "friend_id"
t.string "status"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["friend_id"], name: "index_friendships_on_friend_id"
t.index ["user_id"], name: "index_friendships_on_user_id"
end
create_table "posts", force: :cascade do |t|
t.text "body"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_posts_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "username", default: "", null: false
t.string "name"
t.string "last_name"
t.string "rut", default: "", null: false
t.string "bio"
t.string "uid"
...
end
end
Rails:Rails 5.1.6
Ruby:ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
有什么想法吗? :c 谢谢!
【问题讨论】:
-
您能添加完整的架构吗?
-
@SebastianPalma 不,“朋友”表不存在。朋友毕竟是用户。
-
@SebastianPalma 不,当尝试控制台时,它会抛出同样的错误。
-
@thesecretmaster 现在添加了
-
@SebastianPalma 但是当以以下方式映射类“belongs_to:friend,class_name:”User“”时,我没有以正确的方式进行引用? : c
标签: ruby-on-rails ruby ruby-on-rails-5 material-design-lite