2017-03-10 5 views
0

私はユーザーが1時間のトレーニングで本を作ることができるアプリケーションを作ろうとしています。アクションコントローラーの例外がキャッチされました。予約番号のNoMethodError#new

これは私の予約コントローラです:

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 in Bookings#new Showing /home/cesar/Apps/boxApp/app/views/bookings/new.html.erb where line #6 raised:

undefined method `bookings_path' for <#:0x0055c4717cc4d8> Did you mean? book_path

My 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:すくいroutesコマンドの

<% unless current_user?(@user) %> 
     <% if current_user.bookings(@training) %> 
     <%= link_to "Book", new_training_booking_path(@training), class: "btn btn-primary" %> 

出力:

    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

'すくいroutes'の出力は何を修正する必要がありますように

あなたnew方法を変更しますか? – MikeRogers0

+0

'training_bookings_path(@training)'を試し、 'index'アクションの前にコントローラで' load_training'を実行してください。 パラメータ「のActiveRecord :: BookingsController#でRecordNotFoundが を作成するには 『ID』 =とトレーニングを見つけることができませんでした」: –

+0

training_bookings_pathが – marmeladze

答えて

0

NoMethodError in Bookings#new Showing /home/cesar/Apps/boxApp/app/views/bookings/new.html.erb where line #6 raised:

undefined method `bookings_path' for <#:0x0055c4717cc4d8> Did you mean? book_path

あなたがで<%= form_for ([@training, @booking]) do |f| %>を持っていますnew.html.erbであるが、@trainingであり、newである。 before_actionを作成の場合のみメソッドに設定しているためです。

before_action :load_training, only: [:create] 

before_action :load_training, only: [:new,:create] 

に変更すると、エラーを修正する必要があります。以下のエラー

def new 
    @booking = Booking.new 
    @training = Training.find(params[:training_id]) 
    @booking.training_id 
end 
+0

OK、それが働いたように見えるが、今私は次のエラーを受け取るべきです: "booking" => {"training_id" => ""}、 "コミット" => "予約確認"、 "training_id" => "11"} training_idを予約に入れないあなたが理由を知っている? –

関連する問題