ruby on rails:undefined方法`_path'

问题描述 投票:1回答:2

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

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

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

意见(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

我的房间控制器(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

被困在这几天,非常感谢帮助:)非常感谢提前!

ruby-on-rails ruby model-view-controller
2个回答
0
投票

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

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

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| %>

Here你可以得到更多关于form_for帮手的细节

© www.soinside.com 2019 - 2024. All rights reserved.