2016-03-22 26 views
0

:Yönlendirme Hata polimorfik

Yönlendirme hatası, başlatılmamış sabit 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 benim roomsshow ilave

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
sık düğme bağlantı kodu. 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) # Çeşitli tututorials izledim ve çok sayıda öneri izlediyseniz

Screenshot of error message with full trace

favorite_rooms) /favorite_rooms/:id(.:format SİL favorite_room_path oluşturmak favorite_rooms ama sorunu kendi başıma çözebiliyor gibi görünmüyor.

+0

olarak değiştirmelisiniz. Lütfen sorunun tamamını yazınız. – Pavan

+0

Sadece eklediğim ekran görüntüsü kapalı hata mesajı – Lee

+0

Dosya adı 'favorite_rooms_controller.rb'dı' favoriterooms_controller.rb' olmalıdır. Değiştirmeyi dene. – Pavan

cevap

0

Yönlendirme hatası, başlatılmamış sabit FavoriteRoomsController

Raylar çok sıkı ve iyi bir neden için adlandırma kurallarına uymanızı. Dosya adlarının yılan kasasında olması beklenir. Bu nedenle, favoriterooms_controller.rb dosya adını favorite_rooms_controller.rb