Skip to main content
コードに変更
ソース リンク
raccy
  • 8,272
  • 1
  • 14
  • 41

rails s
=> Booting Puma
=> Rails 6.1.7 application starting in development
=> Run bin/rails server --help for more startup options
Exiting
/home/ec2-user/.rvm/gems/ruby-3.1.2/gems/activesupport-6.1.7/lib/active_support/core_ext/hash/keys.rb:52:in `block in assert_valid_keys': Unknown key: :souce. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :before_add, :after_add, :before_remove, :after_remove, :extend, :counter_cache, :join_table, :index_errors, :ensuring_owner_was, :through, :source, :source_type (ArgumentError)

rails s
=> Booting Puma
=> Rails 6.1.7 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
/home/ec2-user/.rvm/gems/ruby-3.1.2/gems/activesupport-6.1.7/lib/active_support/core_ext/hash/keys.rb:52:in `block in assert_valid_keys': Unknown key: :souce. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :before_add, :after_add, :before_remove, :after_remove, :extend, :counter_cache, :join_table, :index_errors, :ensuring_owner_was, :through, :source, :source_type (ArgumentError)

relationships.controller
class RelationshipsController < ApplicationController

def create
@user = User.find(params[:user_id])
current_user.follow(params[:user_id])
redirect_to request.referer
end

def destroy
current_user.unfollow(params[:user_id])
redirect_to request.referer
end

def followings
user = User.find(params[:user_id])
@users = user.followings
end

def followers
user = User.find(params[:user_id])
@users = user.followers
end
end

class RelationshipsController < ApplicationController

  def create
    @user = User.find(params[:user_id])
    current_user.follow(params[:user_id])
    redirect_to request.referer
  end

  def destroy
    current_user.unfollow(params[:user_id])
    redirect_to request.referer
  end

  
  def followings
    user = User.find(params[:user_id])
    @users = user.followings
  end
 
 def followers
   user = User.find(params[:user_id])
   @users = user.followers
 end
end

relationship.rb
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
end

user.rb
has_many :relationships, class_name: "Relationship", foreign_key:
"follower_id", dependent: :destroy
has_many :reverse_of_relationships, class_name: "Relationship",
foreign_key: "followed_id", dependent: :destroy
has_many :followings, through: :relationships, souce: :followed
has_many:followers,through::reverse_of_relationships,souce::follower

def follow(user_id)
relationships.create(followed_id: user_id)
end

class Relationship < ApplicationRecord
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
end

def unfollow(user_id)
relationships.find_by(followed_id: user_id).destroy
endrb

def following?(user)
followings.include?(user)
end
end

has_many :relationships, class_name: "Relationship", foreign_key: 
  "follower_id", dependent: :destroy
has_many :reverse_of_relationships, class_name: "Relationship", 
  foreign_key: "followed_id", dependent: :destroy
has_many :followings, through: :relationships, souce: :followed
  has_many:followers,through::reverse_of_relationships,souce::follower

def follow(user_id)
  relationships.create(followed_id: user_id)
  end

  def unfollow(user_id)
  relationships.find_by(followed_id: user_id).destroy
  end
  
  def following?(user)
  followings.include?(user)
  end
end

routes.rb
resources :users do
resource :relationships, only: [:create, :destroy]
get 'folloeings' => 'relationships#followings', as: 'followings'
get 'followers' => 'relationships#followers' , as: 'followers'
end

 resources :users do
    resource :relationships, only: [:create, :destroy]
    get 'folloeings' => 'relationships#followings', as: 'followings'
    get 'followers' => 'relationships#followers' , as: 'followers'
  end

migration file
class CreateRelationships < ActiveRecord::Migration[6.1]
def change
create_table :relationships do |t|
t.integer :follower_id
t.integer :followed_id
t.timestamps
end
end
end

class CreateRelationships < ActiveRecord::Migration[6.1]
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id
      t.timestamps
    end
  end
end

rails s
=> Booting Puma
=> Rails 6.1.7 application starting in development
=> Run bin/rails server --help for more startup options
Exiting
/home/ec2-user/.rvm/gems/ruby-3.1.2/gems/activesupport-6.1.7/lib/active_support/core_ext/hash/keys.rb:52:in `block in assert_valid_keys': Unknown key: :souce. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :before_add, :after_add, :before_remove, :after_remove, :extend, :counter_cache, :join_table, :index_errors, :ensuring_owner_was, :through, :source, :source_type (ArgumentError)

relationships.controller
class RelationshipsController < ApplicationController

def create
@user = User.find(params[:user_id])
current_user.follow(params[:user_id])
redirect_to request.referer
end

def destroy
current_user.unfollow(params[:user_id])
redirect_to request.referer
end

def followings
user = User.find(params[:user_id])
@users = user.followings
end

def followers
user = User.find(params[:user_id])
@users = user.followers
end
end

relationship.rb
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
end

user.rb
has_many :relationships, class_name: "Relationship", foreign_key:
"follower_id", dependent: :destroy
has_many :reverse_of_relationships, class_name: "Relationship",
foreign_key: "followed_id", dependent: :destroy
has_many :followings, through: :relationships, souce: :followed
has_many:followers,through::reverse_of_relationships,souce::follower

def follow(user_id)
relationships.create(followed_id: user_id)
end

def unfollow(user_id)
relationships.find_by(followed_id: user_id).destroy
end

def following?(user)
followings.include?(user)
end
end

routes.rb
resources :users do
resource :relationships, only: [:create, :destroy]
get 'folloeings' => 'relationships#followings', as: 'followings'
get 'followers' => 'relationships#followers' , as: 'followers'
end

migration file
class CreateRelationships < ActiveRecord::Migration[6.1]
def change
create_table :relationships do |t|
t.integer :follower_id
t.integer :followed_id
t.timestamps
end
end
end

rails s
=> Booting Puma
=> Rails 6.1.7 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
/home/ec2-user/.rvm/gems/ruby-3.1.2/gems/activesupport-6.1.7/lib/active_support/core_ext/hash/keys.rb:52:in `block in assert_valid_keys': Unknown key: :souce. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :before_add, :after_add, :before_remove, :after_remove, :extend, :counter_cache, :join_table, :index_errors, :ensuring_owner_was, :through, :source, :source_type (ArgumentError)

relationships.controller

class RelationshipsController < ApplicationController

  def create
    @user = User.find(params[:user_id])
    current_user.follow(params[:user_id])
    redirect_to request.referer
  end

  def destroy
    current_user.unfollow(params[:user_id])
    redirect_to request.referer
  end

  
  def followings
    user = User.find(params[:user_id])
    @users = user.followings
  end
 
 def followers
   user = User.find(params[:user_id])
   @users = user.followers
 end
end

relationship.rb

class Relationship < ApplicationRecord
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
end

user.rb

has_many :relationships, class_name: "Relationship", foreign_key: 
  "follower_id", dependent: :destroy
has_many :reverse_of_relationships, class_name: "Relationship", 
  foreign_key: "followed_id", dependent: :destroy
has_many :followings, through: :relationships, souce: :followed
  has_many:followers,through::reverse_of_relationships,souce::follower

def follow(user_id)
  relationships.create(followed_id: user_id)
  end

  def unfollow(user_id)
  relationships.find_by(followed_id: user_id).destroy
  end
  
  def following?(user)
  followings.include?(user)
  end
end

routes.rb

 resources :users do
    resource :relationships, only: [:create, :destroy]
    get 'folloeings' => 'relationships#followings', as: 'followings'
    get 'followers' => 'relationships#followers' , as: 'followers'
  end

migration file

class CreateRelationships < ActiveRecord::Migration[6.1]
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id
      t.timestamps
    end
  end
end
ソース リンク
pon
  • 15
  • 3

フォロー機能を実装しようとしたらrails sできない

relationshipフォロー、フォロワー機能を記述してrails s しようとしたらサーバーが立ち上がらなくなりました。 どこかの引数が間違っているみたいなのですがどこを直して良いかわかりません。

rails s
=> Booting Puma
=> Rails 6.1.7 application starting in development
=> Run bin/rails server --help for more startup options
Exiting
/home/ec2-user/.rvm/gems/ruby-3.1.2/gems/activesupport-6.1.7/lib/active_support/core_ext/hash/keys.rb:52:in `block in assert_valid_keys': Unknown key: :souce. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :before_add, :after_add, :before_remove, :after_remove, :extend, :counter_cache, :join_table, :index_errors, :ensuring_owner_was, :through, :source, :source_type (ArgumentError)

以下、該当していそうなコードです。初学者なので見づらい部分があると思いますが
よろしくお願いします。

relationships.controller
class RelationshipsController < ApplicationController

def create
@user = User.find(params[:user_id])
current_user.follow(params[:user_id])
redirect_to request.referer
end

def destroy
current_user.unfollow(params[:user_id])
redirect_to request.referer
end

def followings
user = User.find(params[:user_id])
@users = user.followings
end

def followers
user = User.find(params[:user_id])
@users = user.followers
end
end

relationship.rb
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
end

user.rb
has_many :relationships, class_name: "Relationship", foreign_key:
"follower_id", dependent: :destroy
has_many :reverse_of_relationships, class_name: "Relationship",
foreign_key: "followed_id", dependent: :destroy
has_many :followings, through: :relationships, souce: :followed
has_many:followers,through::reverse_of_relationships,souce::follower

def follow(user_id)
relationships.create(followed_id: user_id)
end

def unfollow(user_id)
relationships.find_by(followed_id: user_id).destroy
end

def following?(user)
followings.include?(user)
end
end

routes.rb
resources :users do
resource :relationships, only: [:create, :destroy]
get 'folloeings' => 'relationships#followings', as: 'followings'
get 'followers' => 'relationships#followers' , as: 'followers'
end

migration file
class CreateRelationships < ActiveRecord::Migration[6.1]
def change
create_table :relationships do |t|
t.integer :follower_id
t.integer :followed_id
t.timestamps
end
end
end