ruby on rails:未定义的方法`_path'

时间:2018-01-27 20:44:03

标签: ruby-on-rails ruby model-view-controller

当我尝试创建新房间时,我从第7行的错误中收到错误:

undefined method `rooms_path' for #<#<Class:0x007fb46b5db2a0>:0x007fb46b5d9ec8>

我对为什么会收到此错误感到有点困惑。我在RoomFacility之间建立了一种关系,即每个工厂都有很多房间。 Facility部分无错误,我已经能够创建/编辑/显示设施。

观看次数(views / rooms / new.html.erb)

<div class="panel panel-default">
  <div class="panel-heading">
    Create your beautiful room
  </div>
  <div class="panel-body">
    <div class="container">
      <%= form_for @room do |f| %>
        <div class="form-group">
          <label>Name</label>
          <%=f.text_field :room_name, placeholder: "What is the name of the room?", class: "form-control", required: true%>
        </div>

        <div><%= f.submit "Create This Room", class: "btn btn-normal" %></div>

      <%end%>
    </div>
  </div>
</div>

我的房间型号(型号/ room.rb):

class Room < ApplicationRecord
  belongs_to :facility

  validates :room_type, presence: true
  validates :room_name, presence: true
end

我的设施模型(models / facility.rb):

class Facility < ApplicationRecord
  belongs_to :user
  has_many :photos
  has_many :rooms

  validates :facility_name, presence: true
  validates :address, presence: true
  validates :license, presence: true
  validates :phone, presence: true

  def cover_photo(size)
    if self.photos.length > 0
      self.photos[0].image.url(size)
    else
      "blank.jpg"
    end
  end
  
end

My Rooms Controller(controllers / rooms_controller.rb)

class RoomsController < ApplicationController
  before_action :set_room, except: [:index, :new, :create]
  
  def index
    @facility = Facility.find(params[:facility_id])
    @rooms = @facility.rooms
  end

  def new
    @facility = Facility.find(params[:facility_id])
    @room = @facility.rooms.build
  end
 end

路线(routes.rb)

Rails.application.routes.draw do
  devise_for :users,
              path: '',
              path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
              controllers: {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations'}

  resources :users, only: [:show]
  resources :facilities, except: [:edit] do
    member  do
      get 'listing'
      get 'pricing'
      get 'features'
      get 'services'
      get 'types_care'
      get 'photo_upload'
      get 'room_upload'
    end

    resources :rooms, except: [:edit] do
      member  do
        get 'listing'
        get 'pricing'
        get 'photo_upload'
      end
    end

    resources :photos, only: [:create, :destroy]
  end
end

已经坚持了几天,非常感谢帮助:)非常感谢!

2 个答案:

答案 0 :(得分:0)

form_for根据@room类生成网址,但不包含外部资源。因此,您必须告诉Rails使用以下语句明确地执行此操作:

<%= form_for [@room.facility, @room] do |f| %>

答案 1 :(得分:0)

选择form_for代替<%= form_for @room do |f| %>以下任何内容,因为您的路线已嵌套

<%= form_for @room, url: [@room.facility, @room] do |f| %>

<%= form_for @room, url: [:facility, @room] do |f| %>

<%= form_for [@room.facility, @room] do |f| %>

<%= form_for [:facility, @room] do |f| %>

process substitution您可以获得有关form_for帮助

的更多详细信息