我正在尝试创建一个应用,用户可以在一小时的培训中制作一本书。
这是我的预订控制器:
class BookingsController < ApplicationController
before_action :load_training, only: [:create]
def new
@booking = Booking.new
@booking.training_id
end
def create
@booking = @training.bookings.build(booking_params)
@booking.user = current_user.id
if @booking.save
flash[:success] = "Book created"
redirect_to training_index_url
else
render 'new'
end
end
def index
@booking = Booking.all
end
def destroy
@booking = Booking.find(params[:id])
@booking.destroy
flash[:success] = "Book deleted"
redirect_to training_index_url
end
private
def booking_params
params.require(:booking).permit(:user_id, :training_id)
end
def load_training
@training = Training.find(params[:booking][:training_id])
end
end
这是我的预订模式:
class Booking < ApplicationRecord
belongs_to :user
belongs_to :training
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :training_id, presence: true
end
当我尝试在训练中制作本书时,我收到以下错误:
预订中的NoMethodError #new Showing /home/cesar/Apps/boxApp/app/views/bookings/new.html.erb其中第6行 提出:
未定义的方法`bookings_path'for &LT;#:0x0055c4717cc4d8&GT;你的意思是? book_path
我的routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get '/signup', to: 'users#new'
get '/contact', to: 'static_pages#contact'
get '/about', to: 'static_pages#about'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/book', to: 'bookings#new'
post '/book', to: 'bookings#create'
delete '/unbook', to: 'bookings#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :trainings do
resources :bookings
end
resources :users
end
我的new.html.erb预订视图:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for ([@training, @booking]) do |f| %>
<%= f.hidden_field :training_id %>
<%= f.submit "Confirm book", class: "btn btn-primary" %>
<% end %>
我想现在为什么我会收到此错误,如何修复它以便成功完成本书。
show.html.erb培训视图:
<% provide(:title, 'Clase | Ludus app') %>
<div class="row">
<section>
<h1>
HOUR: <%= @training.hour %>
</h1>
</section>
<section>
<h1>
SLOTS: <%= @training.slots %>
</h1>
</section>
<center>
<%= render 'bookings/booking_form' if logged_in? %>
<%= render 'bookings/show_bookings' if logged_in? %>
</center>
booking_form:
<% unless current_user?(@user) %>
<% if current_user.bookings(@training) %>
<%= link_to "Book", new_training_booking_path(@training), class: "btn btn-primary" %>
rake routes命令输出:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
signup GET /signup(.:format) users#new
contact GET /contact(.:format) static_pages#contact
about GET /about(.:format) static_pages#about
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
book GET /book(.:format) bookings#new
POST /book(.:format) bookings#create
unbook DELETE /unbook(.:format) bookings#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
training_bookings GET /trainings/:training_id/bookings(.:format) bookings#index
POST /trainings/:training_id/bookings(.:format) bookings#create
new_training_booking GET /trainings/:training_id/bookings/new(.:format) bookings#new
edit_training_booking GET /trainings/:training_id/bookings/:id/edit(.:format) bookings#edit
training_booking GET /trainings/:training_id/bookings/:id(.:format) bookings#show
PATCH /trainings/:training_id/bookings/:id(.:format) bookings#update
PUT /trainings/:training_id/bookings/:id(.:format) bookings#update
DELETE /trainings/:training_id/bookings/:id(.:format) bookings#destroy
trainings GET /trainings(.:format) trainings#index
POST /trainings(.:format) trainings#create
new_training GET /trainings/new(.:format) trainings#new
edit_training GET /trainings/:id/edit(.:format) trainings#edit
training GET /trainings/:id(.:format) trainings#show
PATCH /trainings/:id(.:format) trainings#update
PUT /trainings/:id(.:format) trainings#update
DELETE /trainings/:id(.:format) trainings#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
答案 0 :(得分:0)
预订中的NoMethodError #new Showing /home/cesar/Apps/boxApp/app/views/bookings/new.html.erb其中第6行 提出:
未定义方法`bookings_path'for&lt;#:0x0055c4717cc4d8&gt;你是否 意思? book_path
<%= form_for ([@training, @booking]) do |f| %>
中new.html.erb
@training
,但new
<{1}} 不可用 before_action
方法。 ,因为您只为创建 方法设置了 before_action :load_training, only: [:create]
。
更改
before_action :load_training, only: [:new,:create]
到
new
应该修复错误。
更改下面的def new
@booking = Booking.new
@training = Training.find(params[:training_id])
@booking.training_id
end
方法应该可以解决错误
apply Service for (disk_name => config in host.vars.disks) to Host {
import "generic-service"
vars = vars + config
display_name = disk_name
check_command = "disk"
}