使用Rails收藏夹路由错误,多态

时间:2016-03-22 17:36:49

标签: ruby-on-rails-4

按照向我现有项目添加收藏夹的教程,用户可以收藏属性列表,但无法通过以下错误:

路由错误,未初始化的常量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

class Room < ActiveRecord::Base
  belongs_to :user
  has_many :photos
  has_many :favorites


  geocoded_by :address
  after_validation :geocode, if: :address_changed?

  validates :home_type, presence: true
  validates :room_type, presence: true
  validates :accommodate, presence: true
  validates :bed_room, presence: true
  validates :bath_room, presence: true
  validates :listing_name, presence: true, length: {maximum: 50}
  validates :summary, presence: true, length: {maximum: 500}
  validates :address, presence: true
  validates :lister_type, presence: true
  validates :gender_type, presence: true
  validates :occupation_type, presence: true
  validates :move_in, presence: true
  validates :term, presence: true
  validates :term, presence: true
end

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
我的roomsshow.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)favorite_rooms #create favorite_room_path DELETE /favorite_rooms/:id(.:format)favorite_rooms

Screenshot of error message with full trace

我看过各种各样的教程并遵循了许多建议,但似乎无法自己解决问题。

1 个答案:

答案 0 :(得分:0)

  

路由错误,未初始化的常量FavoriteRoomsController

Rails非常严格地遵循命名约定,这是有充分理由的。它希望文件名在 蛇案例 中。因此,您应将文件名favoriterooms_controller.rb更改为favorite_rooms_controller.rb