【问题标题】:Active record not found - Couldnt find Training with 'id'=找不到活动记录 - 找不到带有“id”的培训=
【发布时间】:2017-03-09 19:52:14
【问题描述】:

当用户可以预订培训时间时,我正在尝试制作应用程序。 当我尝试创建这本书时,我收到以下错误:

ActiveRecord::RecordNotFound in BookingsController#create

找不到带有 '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

我不知道为什么我尝试做一本新书时它不带 training_id 参数

当我将 @training 和 @user 放入 create 方法时,它返回 nil。我想知道为什么?我的代码在哪里失败,所以预订模型没有检索到 user_id 和 training_id

这是我的预订模式:

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

新的预订视图:

<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(当我执行 p @user 它返回 nil 时)

这是预订表格:

<% 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 %>

当在预订表格中点击“预订”时,命令显示:

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'=):

谢谢

【问题讨论】:

    标签: ruby-on-rails model-view-controller


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      您将发送training_id 作为预订参数的一部分:

      params.require(:booking).permit(:user_id, :training_id)
      

      所以你应该这样做:

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

      另外,您需要将其实际添加到表单中,因此此时应设置@booking.training_id(在控制器new 操作中)并且您需要将其添加到表单中:

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

      【讨论】:

        猜你喜欢
        • 2013-09-06
        • 2021-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-05
        • 2015-08-28
        相关资源
        最近更新 更多