dimanche 24 juillet 2016

Select column from different model and show attributes in view

I have a scaffolded 'Customer.rb' model and a 'Invoice.rb' model. I want people to select a existing Customer from the Customer model and put a few attributes of that selected option in the view of the invoice.

This is the code inside my Invoice#new (@customer = Customer.all in the Invoice_controller)

                <%= @customer.select(:company_name) do |f| %>
                    <h4>Customer:</h4>
                    <div class="well">
                      <address>
                        <strong class="text-dark"><%= f.company_name %></strong><br/>
                        <%= f.first_name + f.last_name if f.first_name.present? && f.last_name.present?%><br/>
                        <%= f.address_line_1 %><br/>
                        <%= f.address_line_2 if present? %>
                      </address>
                    </div>
                <% end %>
              </div>

This shows the correct information but it shows it for ALL columns in the Customers table. I wan't people to pick a specific column only. Like lets say; someone selects 'Coca Cola Ltd.' from a dropdown and then sees only the information of Coca Cola Ltd. (f.company_name: Coca Cola Ltd. etcetera)

The Invoice.rb model contains 'has_one :customer'
The Customer.rb model contains 'belongs_to :invoice'

I tried everything from the ruby on rails guides but i don't seem to get it. Any help would be MUCH appreciated

Aucun commentaire:

Enregistrer un commentaire