Fields_for doesnt working

I’am working on a project about a booking system for a hotel, i have the
Client model and the Reservation model, the client has_many reservations
and the reservation belongs_to :client.

The problem si that when i want to create a new reservation, i want to
be
able to create a client in the same form…

Here is the form:

<%= form_for(@reserva) do |f| %>
<% if @reserva.errors.any? %>


<%= pluralize(@reserva.errors.count, “error”)
%>
prohibited this reserva from being saved:



    <% @reserva.errors.full_messages.each do |msg|
    %>
  • <%= msg %>

  • <% end %>


<% end %>

<%= f.label :cliente_id %>
<% clientes_array = Cliente.all.map { |cliente|
["#{cliente.nombre} #{cliente.apellido}", cliente.id] } %>
<%= f.select(:cliente_id,
options_for_select(clientes_array)) %>


Cliente Nuevo?
<%= check_box_tag ‘con_cliente’, ‘ClienteNuevo’,
false
%>
            <div id="cliente_form">
                <%= f.fields_for :cliente  do |cliente_builder| %>
                  <div class="span12 field-box">
                    <label>Nombre:</label>
                    <%= cliente_builder.text_field(:nombre, class:

“span9”) %>

and here is the controller:

class ChecksController < ApplicationController
def checkin
@reserva = Reserva.new
cliente = @reserva.build_cliente
end

when i try to add a reservation, i complete the client fields_for but
when
the reservation is created, the client is equal to nil…

Any ideas ??

On 18 April 2014 00:34, Juan Andres R. [email protected]
wrote:

I’am working on a project about a booking system for a hotel, i have the
Client model and the Reservation model, the client has_many reservations and
the reservation belongs_to :client.

The problem si that when i want to create a new reservation, i want to be
able to create a client in the same form…

when i try to add a reservation, i complete the client fields_for but when
the reservation is created, the client is equal to nil…

The first rule of debugging is always to identify which section is
going wrong. First look log/development.log where you can see the
parameters passed to the controller. If they look right then you can
start debugging the controller to see where the problem is (you can
use puts statements in the controller code to print data, which will
appear in the server terminal window). If the parameters look wrong
then the problem is with the view, so then inspect the html to see if
that is correct, and so on. Keep splitting the problem down so you
identify a smaller and smaller area where the problem may be. As I
said, this is a fundamental technique of debugging.

Colin

Hi

we need to declare in model

accepts_nested_attributes_for :clientes, allow_destroy: true

in your model and we should declare wrong think

<%= f.fields_for :cliente do |cliente_builder| %>

not a client, we have has_nany relation then we need declare clients

and your controller need build address object such as

@reserva = Reserva.new
1.times{@reserva.clients.builds}

We should declare worng
cliente = @reserva.build_cliente

actualy this is uesd for has_one relation

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs