mercredi 22 juin 2016

collection_select could not pull up list

I was wondering if anybody can help me with this small issue that i am facing.. I am trying to fix my collection_select function on my "address" edit controller.. in my views

edit.html.erb

<%= form_for(@address) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= hidden_field_tag :customer_id, @customer.id %>

<%= f.label :customer_id %>
<%= f.text_field :customer_id, class: 'form-control' %>

<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>

.
.
.

<%= f.label :owner_id %>
<%= f.collection_select :owner_id, @owners, :id, :name, {:include_blank => 'no owners', :prompt => true}, class: 'form-control' %>

.
.
.
<%= f.submit "Edit address", class: "btn btn-primary" %>

in my controller

def edit
@address = Address.find(params[:id])
@customer = Customer.find(params[:id])
@owners = Owner.where(customer_id: @customer.id)
end

This can't work as there will be no owners showing up even thought i have already related them to the specific customer using customer_id in the owners table.

i have tried to change my controller edit code to:

def edit
@address = Address.find(params[:id])
@customer = Customer.find(params[:id])
@owners = Owner.where(customer_id: 75)
end

and this WORKS, as it pulls all owners with customer_id: 75

I have even changed the link into the edit form to include the customer_id into the params like below:

<%= link_to "", edit_address_path(address, :customer_id => @customer.id), remote: true, class: "glyphicon glyphicon-edit" %>

Can anyone help me? will greatly appreaciate as i know it might be just a small error on my coding part.

Aucun commentaire:

Enregistrer un commentaire