Trying to make a form using nested attributes

前端 未结 1 523
清歌不尽
清歌不尽 2021-01-27 16:59

I\'m a beginner in rails, so I\'m quite stuck with the nested attributes.

Here is an overview of what I\'m looking for:

A rentalcar can create cars.(done)

1条回答
  •  耶瑟儿~
    2021-01-27 17:49

    I believe your relationships are a bit messed up, this is how I created new relations.

    class AddRelevantModels < ActiveRecord::Migration
      def change
        create_table :car_rentals do |t|
          t.string      :name
          t.timestamps  null: false
        end
    
        create_table :cars do |t|
          t.string      :model
          t.string      :car_number
          t.belongs_to  :car_rental, index: true, foreign_key: true
          t.timestamps  null: false
        end
    
        create_table :clients do |t|
          t.string      :full_name
          t.integer     :age
          t.string      :email
          t.string      :phone_number
          t.timestamps  null: false
        end
    
        create_table :reservations do |t|
          t.belongs_to    :car, index: true, foreign_key: true
          t.belongs_to    :client, index: true, foreign_key: true
          t.datetime      :start_date
          t.datetime      :end_date
          t.timestamps    null: false
        end
      end
    end
    

    car_rental.rb

    class CarRental < ActiveRecord::Base
      has_many :cars
    end
    

    car.rb

    class Car < ActiveRecord::Base
      has_many :reservations
      has_many :clients, through: :reservations
    end
    

    reservation.rb

    class Reservation < ActiveRecord::Base 
      belongs_to :client
      belongs_to :car
    
      accepts_nested_attributes_for :client
      accepts_nested_attributes_for :car
    end
    

    client.rb

    class Client < ActiveRecord::Base
      has_many :reservations
      has_many :cars, through: :reservations
    end
    

    reservations_controller.rb

    class ReservationsController < ApplicationController
      def new
        @reservation = Reservation.new
        @reservation.build_client
      end
    
      def create
        @reversation = Reservation.new(reservation_params)
    
        if @reversation.save
          render :show, id: @reservation
        else
          render :new
        end
      end
    
      private
    
      def reservation_params
        params.require(:reservation)
        .permit(
          :start_date, :end_date, client_attributes: [:full_name, :age, :email, :phone_number]
        )
      end
    end
    

    reservations/new.html.erb

    Reservations

    <%= form_for(@reservation) do |f| %> <%= f.label :start_date %> <%= f.text_field :start_date %>
    <%= f.label :end_date %> <%= f.text_field :end_date %>
    <%= f.fields_for :client do |client_field| %> <%= client_field.label :full_name %> <%= client_field.text_field :full_name %>
    <%= client_field.label :age %> <%= client_field.text_field :age %>
    <%= client_field.label :email %> <%= client_field.text_field :email %>
    <%= client_field.label :phone_number %> <%= client_field.text_field :phone_number %> <% end %>
    <%= f.submit %> <% end %>

    Now if you submit the form you can see in your database that the client is also saved for the reservation.

     Reservation.first.client
      Reservation Load (0.5ms)  SELECT  "reservations".* FROM "reservations"   ORDER BY "reservations"."id" ASC LIMIT 1
      Client Load (0.4ms)  SELECT  "clients".* FROM "clients"  WHERE "clients"."id" = $1 LIMIT 1  [["id", 1]]
     => #
    

    0 讨论(0)
提交回复
热议问题