ルーティングエラー、初期化されていない一定のFavoriteRoomsController
favoriterooms_controller.rb
class FavoriteRoomsController < ApplicationController
before_action :set_room
def create
if Favorite.create(favorited: @room, user: current_user)
redirect_to @room, notice: 'Room has been favorited'
else
redirect_to @room, alert: 'Something went wrong...*sad panda*'
end
end
def destroy
Favorite.where(favorited_id: @room.id, user_id: current_user.id).first.destroy
redirect_to @room, notice: 'Room is no longer in favorites'
end
private
def set_room
@room = Room.find(params[:room_id] || params[:id])
end
end
room.rb
012私roomsshowに追加user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
validates :fullname, presence: true, length: {maximum: 50}
has_many :rooms
has_many :favorites
has_many :favorite_rooms, through: :favorites, source: :favorited, source_type: 'Room'
def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first
if user
return user
else
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.fullname = auth.info.name
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.image = auth.info.image
user.password = Devise.friendly_token[0,20]
end
end
end
end
favorite.rb
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :favorited, polymorphic: true
end
routes.rbを
Rails.application.routes.draw do
root 'pages#home'
devise_for :users,
:path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile'},
:controllers => {:omniauth_callbacks => 'omniauth_callbacks',
:registrations => 'registrations'
}
resources :users, only: [:show]
resources :rooms
resources :photos
resources :conversations, only: [:index, :create] do
resources :messages, only: [:index, :create]
end
resources :favorite_rooms, only: [:create, :destroy]
end
roomshow.html.erb
お気に入りボタンのリンクコード。 html.erb
<%- unless current_user.favorite_rooms.exists?(id: @room.id) -%>
<%= link_to 'Add to favorites', favorite_rooms_path(room_id: @room), method: :post %>
<%- else -%>
<%= link_to 'Remove from favorites', favorite_room_path(@room), method: :delete %>
<%- end -%>
favorite_rooms_pathのPOSTの/favorite_rooms(.:format)は、#私は様々なtututorialsを見て、数多くの提案に従ってきた
Screenshot of error message with full trace
favorite_rooms)/favorite_rooms/:id(.:formatをDELETE favorite_room_path を作成favorite_rooms自分で問題を解決することはできないようです。
質問に完全なエラーを投稿してください。 – Pavan
エラーメッセージ – Lee
のスクリーンショットを追加しました。ファイル名は 'favoriterooms_controller.rb'ではなく' favorite_rooms_controller.rb'でなければなりません。それを変更してみてください。 – Pavan