how to see a different content?

L'action create of deliveries controller is:

def create    @customer=Customer.find(params[:customer_id])    @delivery = @customer.deliveries.build(params[:delivery])    @document = @customer.build_document(params[:document])    if @delivery.valid? and @document.valid?      Delivery.transaction do        @delivery.save!        @document.save!      end      flash[:success] = 'Ok'      respond_with(@customer)    else      @products = Product.all      render 'customers/show', :layout => 'delivery'    end end

l'action show of customers controller is:

............. <% content_for :delivery_form do %> <%= render 'deliveries/form' %> <% end %> <% content_for :delivered do %> <%= render 'delivered/form' %> <% end %>

I want to see only one of the two content_for depending on @delivery and @document are saved or not. I have a form, on submit @delivery and @document are created, if they are not valid I have to see the show view and in the layout I should see <%= yield :delivery_form %> while if the objects are valid eand then saved in the database in the layout I should see <%= yield :delivered %>. Based on what I can make the selection?

Any advice? May be there is something wrong?

You could set an @variable in the controller action and test this in the view to decide what to show.

Colin

Can't I test if an object is saved or not in the database?

Well an unsaved object will have an id of nil.

Do you mean test whether it is saved ok in the create action? You know whether it saved ok by the path it took through the action.

Colin

or you can use the ".new_record?" method.

I'll try soon.

It' s not the solution. After I've saved @document and @delivery I go to show action where I have @delivery=Delivery.new and @document=Document.new, so new_record is always true.

In the show action you should be finding the records in the database, not making new ones. Otherwise how are you going to show them?

Colin

I show customer and in the same view I need a form to create a delivery and a document associated to that customer:

This is customer show view:

<p>   <b><%= Customer.human_attribute_name("year") %>:</b>   <%= @customer.year %> </p>

<p>   <b><%= Customer.human_attribute_name("code") %>:</b>   <%= @customer.code %> </p>

<p>   <b><%= Customer.human_attribute_name("full_name") %>:</b>   <%= @customer.full_name %> </p>

<p>   <b><%= Customer.human_attribute_name("birth_date") %>:</b>   <% if @customer.birth_date %>       <%= l @customer.birth_date %>     <% else %>       <%= @customer.birth_date %>     <% end %> </p>

<% content_for :delivery_form do %>   <%= render 'deliveries/form' %> <% end %>

<% content_for :delivered do %>   <p>   <% if @delivery.valid? and @document.valid? %>     <% for product in @customer.deliveries.last.products %>       <%= product.description %><br />     <% end %>   <% end %>   </p>   <p class="rightText">     <button id="print_button">stampa</button>   </p> <% end %>

The delivery_form partial is:

<%= simple_form_for([@customer, @delivery]) do |f| %>     <p>       <%= f.input :delivered_at, :as => :hidden, :input_html => { :value => Date.today } %>     </p>      <p>       <% for product in @products %>         <%= check_box_tag 'delivery[product_ids]', product.id, @delivery.products.include?(product) %>         <%= product.description %><br />       <% end %>       <%= f.error :products%>     </p><br />     <%= simple_fields_for @document do |doc| %>       <p>         <%= doc.label :doc_type %>:<br />         <%= doc.text_field :doc_type %>         <%= doc.error :doc_type %>       </p>       <p>         <%= doc.label :doc_number %>:<br />         <%= doc.text_field :doc_number %>         <%= doc.error :doc_number %>       </p>       <p>         <%= doc.label :issued_by %>:<br />         <%= doc.text_field :issued_by %>         <%= doc.error :issued_by %>       </p>     <% end %>

As you see I need @delivery and @document object and for that in the customer show action I've to do

def show     @customer = Customer.find(params[:id])     @delivery = Delivery.new     @document = Document.new     @products = Product.all   end