Get And Display All Values From Database Row Separately Based On Select (rails)
Hi i'm stuck making an invoicing application. I want to have an option where you can add an existing customer from the customers table and add those values to the invoice. The valu
Solution 1:
<%= select_tag 'choose customer', options_from_collection_for_select(current_user.customers.all, 'id', 'company_name'), id: "choose_customer" %>
You can bind the change event from the rails to an ajax call, send the id of the selected object, if you want to fetch the data based on the selected value, call a method, then handle the view using that response
$(document).ready(function() {
$("#choose_company").bind("change",function() {
if ($(this).val() != undefined) {
$.ajax({
url : "/my_action",
data: {'company': $(this).val()},
dataType: "json",
type: "GET",
success : function(data) {
$(".display_btw").html(data["btw_number"]);
$(".display_iban").html(data["iban_number"]);
$('.chosen_company_btw').val(data["btw_number"]).text(data["btw_number"]);
$('.chosen_company_iban').val(data["iban_number"]).text(data["iban_number"]);
}
})
}
})
})
Your controller action can be something like,
get '/my_action', to: 'invoices#my_action' add this route.
defmy_actionif params[:company].present?
@company = current_user.companies.find(params[:company])
@data = Hash.new
@data["btw_number"] = @company.btw_number
@data["iban_number"] = @company.iban_number
render json:@dataandreturnfalseendendThis action returns the vat_number and iban_number which should be displayed in the view depending in the selected value.
Solution 2:
Create a method on the customers model that concatenates this information together in a friendly, human-readable way:
deffriendly_info"#{company_name} (#{vat_number}) - IBAN #{iban_number}"endThen use that method in your select statement in place of company_name.
Post a Comment for "Get And Display All Values From Database Row Separately Based On Select (rails)"