2017-03-09 1 views
0

으로 교육을 찾을 수 없습니다. 사용자가 교육 시간을 예약 할 수있을 때 앱을 만들려고합니다. 나는 다음과 같은 오류를받을 책을 만들려고 할 때 : 나는 training_id 매개 변수를 걸립니다 나던 내가 왜이 작업을 수행 할 때 잘 모릅니다활성 레코드가 없습니다. 'id'=

class BookingsController < ApplicationController 
    before_action :load_training, only: [:create] 

    def new 
    @booking = Booking.new 
    end 

    def create 
    @booking = @training.bookings.build(booking_params) 
    @booking.user = current_user 

    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_by(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[:training_id]) 
    end 

end 

:

ActiveRecord::RecordNotFound in BookingsController#create

Couldn't find Training with 'id'=

이 내 예약 컨트롤러 새 책

@training을 넣고 @user를 create 메소드에 넣으면 nil을 반환합니다. 왜 그런지 알고 싶습니까?

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 

내 경로의 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 :bookings,   only: [:create, :destroy] 
    resources :training 
    resources :users 
end 

새로운 예약보기 :

어디에 내 코드는 그래서 USER_ID를 검색하고

을 training_id 나던 예약 모델이 내 예약 모델입니다 실패

<h1>Booking confirmation</h1> 

<%= form_for (@booking) do |f| %> 
    <%= f.submit "Confirm book", class: "btn btn-primary" %> 
<% end %> 

내가 cmd에서 훈련 쇼에 갈 때 :

Started GET "/training/8" for 127.0.0.1 at 2017-03-09 15:39:42 -0400 
Processing by TrainingController#show as HTML 
    Parameters: {"id"=>"8"} 
    Training Load (0.1ms) SELECT "trainings".* FROM "trainings" WHERE "trainings"."id" = ? LIMIT ? [["id", 8], ["LIMIT", 1]] 
    Rendering training/show.html.erb within layouts/application 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] 
DEPRECATION WARNING: Passing an argument to force an association to reload is now deprecated and will be removed in Rails 5.1. Please call `reload` on the result collection proxy instead. (called from _app_views_bookings__booking_form_html_erb__3707831747335467387_70006265917040 at /home/cesar/Apps/boxApp/app/views/bookings/_booking_form.html.erb:4) 
    Booking Load (0.1ms) SELECT "bookings".* FROM "bookings" WHERE "bookings"."user_id" = ? ORDER BY "bookings"."created_at" DESC [["user_id", 2]] 
    Rendered bookings/_booking_form.html.erb (5.9ms) 
    Rendered bookings/_show_bookings.html.erb (0.3ms) 
    Rendered training/show.html.erb within layouts/application (8.4ms) 
    Rendered layouts/_header.html.erb (0.6ms) 
    Rendered layouts/_footer.html.erb (0.2ms) 
Completed 200 OK in 43ms (Views: 34.6ms | ActiveRecord: 0.8ms) 

그것은이 USER_ID 아니라 training_id을 가지고 좋아하는 모양

을 그리고 이것은 booking_form입니다 (이 전무 상점을 반환 @user 나는 P를 수행 할 때) :

<% unless current_user?(@user) %> 
     <% if current_user.bookings(@training) %> 
     <%= link_to "Book", book_path, class: "btn btn-primary" %> 
     <% else %> 
     <%= link_to "Unbook", unbook_path, method: "delete", class: "btn btn-primary" %> 
    <% end %> 
<% end %> 

클릭 " booking_form 예약 "명령을 보여줍니다

Started GET "/book" for 127.0.0.1 at 2017-03-09 15:46:23 -0400 
Processing by BookingsController#new as HTML 
    Rendering bookings/new.html.erb within layouts/application 
    Rendered bookings/new.html.erb within layouts/application (1.8ms) 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] 
    Rendered layouts/_header.html.erb (1.8ms) 
    Rendered layouts/_footer.html.erb (0.7ms) 
Completed 200 OK in 39ms (Views: 37.2ms | ActiveRecord: 0.1ms) 

그리고 난이 책을 확인하려고 할 때 명령이 보여줍니다

Started POST "/bookings" for 127.0.0.1 at 2017-03-09 15:50:25 -0400 
Processing by BookingsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"3NrQ7vA2yL5sIlI3Q5nHiywYy4s3QP20amuu4czTCGmKdYy4fTkY4uMpgEyGxAU/kTDPLcfrsRqJZ1YGpEpnKw==", "commit"=>"Reservar"} 
    Training Load (0.1ms) SELECT "trainings".* FROM "trainings" WHERE "trainings"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]] 
Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms) 



ActiveRecord::RecordNotFound (Couldn't find Training with 'id'=): 

감사

답변

1

시도 코드 아래 :

<%= form_for (@booking) do |f| %> 
    <%= f.hidden_field :user_id, value: current_user.id %> 
    <%= f.hidden_field :training_id, value: "" %> #pass training id in value 
    <%= f.submit "Confirm book", class: "btn btn-primary" %> 
<% end %> 

을 아래 코드로 load_training 기능을 대체는 :

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

당신은 예약 PARAMS의 일환으로 training_id을 보내는이 :

그래서
params.require(:booking).permit(:user_id, :training_id) 

당신이이 일을해야합니다 또한

def load_training 
    @training = Training.find(params[:booking][:training_id]) 
end 

을 수행해야 실제로 양식에 추가하기 때문에이 시점까지 @booking.training_id을 설정해야합니다 (컨트롤러에 있음).3210 액션)와 양식에 추가해야합니다

<%= form_for (@booking) do |f| %> 
    <%= f.hidden_field :training_id %> 
    <%= f.submit "Confirm book", class: "btn btn-primary" %> 
<% end %> 
+0

그것은 나에게 동일하게 제공 오류 –

관련 문제