On 10 February 2011 17:18, Colin Law <[email protected]> wrote:
> On 10 February 2011 16:02, Mauro <[email protected]> wrote:
>> On 10 February 2011 09:56, Mauro <[email protected]> wrote:
>>> On 10 February 2011 09:54, Michael Pavling <[email protected]> wrote:
>>>> On 10 February 2011 09:51, Peter Hickman <[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.
>>
>> 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:

<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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to