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?

On 9 February 2011 11:12, Mauro [email protected] wrote:

end

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?

On 9 February 2011 11:12, Mauro [email protected] wrote:

end

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?

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

Colin

On 10 February 2011 09:31, Colin L. [email protected] wrote:

@document.save!

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?

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

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

Well an unsaved object will have an id of nil.

On 10 February 2011 09:51, Peter H.
[email protected] wrote:

On 10 February 2011 09:37, Mauro

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

Well an unsaved object will have an id of nil.

or you can use the “.new_record?” method.

On 10 February 2011 09:37, Mauro [email protected] wrote:

@delivery.save!
l’action show of customers controller is:
and @document are saved or not.
Can’t I test if an object is saved or not in the database?
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

On 10 February 2011 09:56, Mauro [email protected] wrote:

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.

On 10 February 2011 09:54, Michael P. [email protected] wrote:

On 10 February 2011 09:51, Peter H. [email protected] wrote:

On 10 February 2011 09:37, Mauro

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

Well an unsaved object will have an id of nil.

or you can use the “.new_record?” method.

I’ll try soon.

On 10 February 2011 16:02, Mauro [email protected] wrote:

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

On 10 February 2011 17:18, Colin L. [email protected] wrote:

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?

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:

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

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

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

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

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

<% content_for :delivered do %>

<% if @delivery.valid? and @document.valid? %> <% for product in @customer.deliveries.last.products %> <%= product.description %>
<% end %> <% end %>

stampa

<% end %>

The delivery_form partial is:

<%= simple_form_for([@customer, @delivery]) do |f| %>


<%= f.input :delivered_at, :as => :hidden, :input_html => {
:value => Date.today } %>



<% for product in @products %>
<%= check_box_tag ‘delivery[product_ids][]’, product.id,
@delivery.products.include?(product) %>
<%= product.description %>

<% end %>
<%= f.error :products%>



<%= simple_fields_for @document do |doc| %>


<%= doc.label :doc_type %>:

<%= doc.text_field :doc_type %>
<%= doc.error :doc_type %>



<%= doc.label :doc_number %>:

<%= doc.text_field :doc_number %>
<%= doc.error :doc_number %>



<%= doc.label :issued_by %>:

<%= doc.text_field :issued_by %>
<%= doc.error :issued_by %>


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