下記のサイトをうまく活用して、掲載物に応募ボタンを行うと掲載者とメッセージ機能でやり取りができるようにしたいと思っているのですが、掲載物の閲覧ページ(view/plans/show)に応募ボタンを設置してみたのですが、応募ボタンをクリック後にエラーが発生しました。
https://qiita.com/bindingpry/items/6790c91f374acc25bea2
https://qiita.com/aaaasahi_17/items/9e7f344488c720aaf116
!修正しました。
エラーが発生したファイルがこちらです。
・RoomsController
class RoomsController < ApplicationController before_action :authenticate_user! def create @room = Room.create @entry1 = Entry.create(room_id: @room.id, user_id: current_user.id) @entry2 = Entry.create(params.require(:entry).permit(:user_id, :room_id).merge(room_id: @room.id)) redirect_to "/rooms/#{@room.id}" end def show @room = Room.find(params[:id]) if Entry.where(user_id: current_user.id,room_id: @room.id).present? @messages = @room.messages @message = Message.new @entries = @room.entries else redirect_back(fallback_location: root_path) end end
! モデル/user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable before_save { self.email.downcase! } validates :name, presence: true, length: { maximum: 50 } validates :email, presence: true, length: { maximum: 255 }, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }, uniqueness: { case_sensitive: false } validates :furiganaName, presence: true, length: { maximum: 100 }, format: { with: /\A[ぁ-ん]+\z/u } validates :telephone_number, numericality: { only_integer: true }, length: { maximum:11 }, format: { with: /\A[0-9]+\z/ } validates :password, presence: true, length: { minimum: 6 } validates :self_introduction, presence: false, length: { maximum: 1000 } mount_uploader :image, ImageUploader has_secure_password has_many :plans has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverses_of_relationship, source: :user has_many :favorites, dependent: :destroy has_many :favorite_plans, through: :favorites, source: :plan has_many :entries, dependent: :destroy has_many :messages, dependent: :destroy def follow(other_user) unless self == other_user self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship end def following?(other_user) self.followings.include?(other_user) end def favorite(plan) self.favorites.find_or_create_by(plan_id: plan.id) end def unfavorite(plan) favorite = self.favorites.find_by(plan_id: plan.id) favorite.destroy if favorite end def is_favorite?(plan) self.favorite_plans.include?(plan) end end
!一通りメッセージ機能が形成できましたら、応募済みの方は再度応募ボタンを押せないようにしたいと考えております。
私自身で考えてみたのは、rooms.controllerの
@entry1 = Entry.create(room_id: @room.id, user_id: current_user.id) @entry2 = Entry.create(params.require(:entry).permit(:user_id, :room_id).merge(room_id: @room.id))
のuser_idをplan.user_idにして、掲載者(plan.user)を特定させなければいけないのかなと思ったのですが、エラーが発生してしまいました。
説明や解釈が拙い部分が多くあると思いますが、
ご指導ご鞭撻のほどよろしくお願いいたします。
・追加
application_controller
class ApplicationController < ActionController::Base include SessionsHelper include Pagy::Backend private def require_user_logged_in unless logged_in? redirect_to login_url end end end
NoMethodError in RoomsController#show undefined method `authentication_keys' for User:Class Extracted source (around line #22): 20 21 22 23 24 25 send(name, *arguments, &block) else super end end Rails.root: /home/ubuntu/environment/tsunageru Application Trace | Framework Trace | Full Trace activerecord (6.1.6) lib/active_record/dynamic_matchers.rb:22:in `method_missing' devise (4.8.1) lib/devise/failure_app.rb:104:in `i18n_message' devise (4.8.1) lib/devise/failure_app.rb:84:in `redirect' devise (4.8.1) lib/devise/failure_app.rb:43:in `respond' actionpack (6.1.6) lib/abstract_controller/base.rb:228:in `process_action' actionpack (6.1.6) lib/abstract_controller/base.rb:165:in `process' actionpack (6.1.6) lib/action_controller/metal.rb:190:in `dispatch' actionpack (6.1.6) lib/action_controller/metal.rb:238:in `block in action' devise (4.8.1) lib/devise/failure_app.rb:23:in `call' devise (4.8.1) lib/devise/delegator.rb:7:in `call' warden (1.2.9) lib/warden/manager.rb:143:in `call_failure_app' warden (1.2.9) lib/warden/manager.rb:129:in `process_unauthenticated' warden (1.2.9) lib/warden/manager.rb:44:in `call' rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call' rack (2.2.3) lib/rack/etag.rb:27:in `call' rack (2.2.3) lib/rack/conditional_get.rb:27:in `call' rack (2.2.3) lib/rack/head.rb:12:in `call' actionpack (6.1.6) lib/action_dispatch/http/permissions_policy.rb:22:in `call' actionpack (6.1.6) lib/action_dispatch/http/content_security_policy.rb:19:in `call' rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context' rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/cookies.rb:689:in `call' activerecord (6.1.6) lib/active_record/migration.rb:601:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call' activesupport (6.1.6) lib/active_support/callbacks.rb:98:in `run_callbacks' actionpack (6.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call' web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app' web-console (4.2.0) lib/web_console/middleware.rb:19:in `block in call' web-console (4.2.0) lib/web_console/middleware.rb:17:in `catch' web-console (4.2.0) lib/web_console/middleware.rb:17:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call' railties (6.1.6) lib/rails/rack/logger.rb:37:in `call_app' railties (6.1.6) lib/rails/rack/logger.rb:26:in `block in call' activesupport (6.1.6) lib/active_support/tagged_logging.rb:99:in `block in tagged' activesupport (6.1.6) lib/active_support/tagged_logging.rb:37:in `tagged' activesupport (6.1.6) lib/active_support/tagged_logging.rb:99:in `tagged' railties (6.1.6) lib/rails/rack/logger.rb:26:in `call' sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/remote_ip.rb:81:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/request_id.rb:26:in `call' rack (2.2.3) lib/rack/method_override.rb:24:in `call' rack (2.2.3) lib/rack/runtime.rb:22:in `call' activesupport (6.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/executor.rb:14:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/static.rb:24:in `call' rack (2.2.3) lib/rack/sendfile.rb:110:in `call' actionpack (6.1.6) lib/action_dispatch/middleware/host_authorization.rb:142:in `call' webpacker (5.4.3) lib/webpacker/dev_server_proxy.rb:25:in `perform_request' rack-proxy (0.7.2) lib/rack/proxy.rb:67:in `call' railties (6.1.6) lib/rails/engine.rb:539:in `call' puma (5.6.4) lib/puma/configuration.rb:252:in `call' puma (5.6.4) lib/puma/request.rb:77:in `block in handle_request' puma (5.6.4) lib/puma/thread_pool.rb:340:in `with_force_shutdown' puma (5.6.4) lib/puma/request.rb:76:in `handle_request' puma (5.6.4) lib/puma/server.rb:441:in `process_client' puma (5.6.4) lib/puma/thread_pool.rb:147:in `block in spawn_thread' Request Parameters: {"id"=>"2"} Toggle session dump Toggle env dump Response Headers: None
!修正
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
をuser.rbに追加したときに応募を押した際、deviseのサインインが作動し、ログインが2重になりました。
もしかすると、deviseがいらないのかなとも思い始めました。




回答1件
あなたの回答
tips
プレビュー