ruby on rails:undefined方法`_path'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby on rails:undefined方法`_path'相关的知识,希望对你有一定的参考价值。

当我正在尝试创建一个新房间时,我从第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

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

答案

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

<%= form_for [@room.facility, @room] do |f| %>
另一答案

当你的路线嵌套时,为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帮手的细节

以上是关于ruby on rails:undefined方法`_path'的主要内容,如果未能解决你的问题,请参考以下文章

第一个CRUD的制作方法(Ruby on Rails 開發秘籍 | Ruby on Rails 快速入門)

Ruby on Rails 学习五Ruby语言的方法

第一个Landing Page的制作方法(Ruby on Rails 開發秘籍 | Ruby on Rails 快速入門)

在 Javascript 中执行 ruby​​ 方法 - Ruby on Rails App

Ruby on rails - 静态方法

Ruby on Rails - 如何在 Rails 方法中使用 HTML 坐标?