ich ein Kunden-Modell und book_room Modellaktualisiert Kind Attribute Schienen
class Customer < ApplicationRecord
has_many :book_rooms
accepts_nested_attributes_for :book_rooms
end
class BookRoom < ApplicationRecord
belongs_to :customer
end
im book_room Controller-Methode das schaffen muß, ist von dem übergeordneten
class BookRoomsController < ApplicationController
def create
@customer = Customer.find(params[:customer_id])
@customer_room = @customer.book_rooms.create(book_rooms_params)
flash[:notice] = "Customer has been added to room"
redirect_to customer_path(@customer)
end
def destroy
@customer = Customer.find(params[:customer_id])
@customer_room = @customer.book_rooms.find(params[:id])
@customer_room.destroy
flash[:notice] = "Customer room has been deleted"
redirect_to customer_path(@customer)
end
def edit
@customer = Customer.find(params[:customer_id])
end
def update
@customer = Customer.find(params[:customer_id])
@customer.book_rooms.update(book_rooms_params)
flash[:notice] = "Customer has checked out"
redirect_to @customer
end
private
def book_rooms_params
params.require(:book_room).permit(:room, :first_name, :last_name, :phone_number, :checked_out)
end
Ende
in den Kunden zeigen Seite
<%= form_for [@customer, @customer.book_rooms.build] do |f| %>
<% @room = Room.all %>
<%= f.label "Room: "%>
<%= f.select(:room, @room.collect { |a| [a.room_number, a.id] }) %>
<%= f.submit "Enter" %>
<div class="col-md-12"><%= render @customer.book_rooms.order("created_at DESC") %></div>
Dies funktioniert perfekt und alle untergeordneten Objekte werden erstellt. jetzt, wenn ich versuche, das Kind zu bearbeiten Attribute es nicht bei allen heren dem Bearbeitungsformular in der book_room Editierseite/action
<%= form_for @customer do |f| %>
<%= f.fields_for :book_rooms, @customer.book_rooms do |f| %>
<%= f.check_box :checked_out %>
<% end %>
<%= f.submit "Enter" %>
gibt es etwas, was ich falsch mache
nicht aktualisiert? Warum aktualisiert es nicht?
Kunden Controller
class CustomersController < ApplicationController
before_action :set_customer, only: [:show, :edit, :update, :destroy]
# POST /customers.json
def create
@customer = Customer.new(customer_params)
respond_to do |format|
if @customer.save
format.html { redirect_to @customer, notice: 'Customer was successfully created.' }
format.json { render :show, status: :created, location: @customer }
else
format.html { render :new }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @customer.update(customer_params)
format.html { redirect_to @customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: @customer }
else
format.html { render :edit }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_customer
@customer = Customer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def customer_params
params.require(:customer).permit(:first_name, :last_name, :phone_number, :sex, :book_rooms_attributes => [:checked_out])
end
Sie zeigen nur Ihre 'create' Aktion. Wo ist Ihre' update' Aktion und was ist in Ihrer 'book_rooms_params' Methode? –
Haben Sie einen Kunden-Controller? normalerweise, <% = form_for @customer do | f | %> 'über Kunden/[Kunden_ID] (Update). –
Ok, du musst ': id' in das ': book_rooms_attributes => [: checked_out]' einfügen. Danach können Sie aktualisieren. sonst werden Sie jedes Mal mit neuen Datensätzen belohnt. –