NoMethodError in Reservations # new with a form_for in Ruby on Rails

0

I'm doing a cabin rental system in RoR and I get this error in the view new of Reservations (reservations), which gives when I try to create a form in the view. If I show the empty object that passes the controller does not throw me error, so both routes that I specify below arrive at the view and pass through the controller.

  

NoMethodError in Reservations # new

     

undefined method 'reservations_path' for # < #   : 0x007f73f8791260 >   Did you mean? resolve_asset_path

The view resvervations / new.html.erb is this:

<%= render  'formulario' %>

Which calls the form reservations / _formulario.html.erb :

<%= form_for(@reservation, :html => { :role => "form" }) do |f| %>
  <p>
  <%=f.label :tipo  %>
  <%=f.date_field :tipo %>
  </p>
  <p>
  <%=f.label :description %>
  <%=f.date_field :description %>
  </p>
  <%=f.submit %>
<% end %>

The reservations_controller.rb driver has this:

class ReservationsController < ApplicationController
  def new
    @reservation = Reservation.new
  end

My routes.rb is the following:

Rails.application.routes.draw do

  root 'cabins#index'

  resources :customers, :cabins do
    resource :reservations
  end

  get 'cabins/:id/index', to: 'reservations#index'
  get ':id/new', to: 'reservations#new', as: 'new_reservation'
end

I made two paths that show new , both are redirected from the view cabins / index , which passes @cabin .

The relationship of the models is this:

class Customer < ApplicationRecord
  has_many :reservations, :dependent => :delete_all
  has_many :cabins , through: :reservations
end

class Reservation < ApplicationRecord
  belongs_to :customer
  belongs_to :cabin
  has_one :payment
end

class Cabin < ApplicationRecord
  has_many :reservations, :dependent => :delete_all
  has_many :customers , through:  :reservations, :dependent => :delete_all
  has_many :bicycles, :dependent => :delete_all
  has_many :services, :dependent => :delete_all
end

Thank you very much in advance.

    
asked by Walaleitor 29.04.2018 в 18:07
source

1 answer

0

Since you have a custom route (i.e. get ':id/new', to: 'reservations#new', as: 'new_reservation' ) you have to specify the url in the helper form_for :

<%= form_for(@reservation, url: new_reservation_path, html: { role: "form" }) do |f| %>
    
answered by 29.04.2018 / 19:07
source