populate dropdown with two fields in rails 4 ajax - ruby

I am having trouble populating a field with two columns concatenated. For example: I want to populate Worker field with both first name and last name in AJAX rails 4.
Currently I'm populating with just first name using
<%= collection_select(:work_order ,:worker_id ,Worker.all, :id ,:first_name) %>

You can do something like:
<%= f.input :user_id, :as => :select, :collection => [[current_user.firstname+' ' +current_user.lastname, current_user.id]], :label=>"Names"%>

Related

ActiveAdmin Generic Form Partial

I have the following form for uploading PDFs:
f.inputs 'PDF' do
f.has_many :pdfs, allow_destroy: true do |pdf_f|
pdf_f.semantic_errors
pdf_f.input :name
pdf_f.input :description
pdf_f.input :enabled
pdf_f.input :link, label: 'file', as: :file
end
end
And a way to display the file:
li do
div "name: #{pdf.name}"
div "filename: #{pdf.link.path.split('/').last}"
div raw "description: <br> #{pdf.description}"
div "enabled: #{pdf.enabled ? "yes" : "no"}"
end
Pdf is a polymorphic object thus can appear on several models.
How do I create a generic shard partial for each of them that can then be re-used across ActiveAdmin so I don't have to recreate them every time?
Bear in mind the second one I expect to iterate over it by passing it in as a collection:
For the form answer is to place the form in a partial that resides in:
app/views/active_admin/base/_pdf_form.erb
and then render it like this:
render partial: 'pdf_form', locals: {f: f}
For the display parital you should place the partial at:
app/views/active_admin/base/_pdf.erb
and then render it like this:
render partial: 'pdf', collection: event.pdfs
The partial name needs to be in quotes otherwise rails won't look for it active_admin/base.

ruby form data receive

Guy
this is just simple form in ruby
<%= form_tag("/page/create", method: "get") do %>
<% 5.times do %>
<%= text_field_tag :Name,(params[:Name]), size: 10 %><br>
<% end %>
<%= submit_tag("send") %>
<% end%>
why in my control the #Name=params['Name'] not work with me well ?
always not gives me any thing ?
You are posting the same paramter multiple times. This leads to an empty params['Name'], except you fill out the last of the input fields.
Most frameworks discard multiple parameters with the same name, using only the last one in the parameter list.
If you want to pass an array of fields to your controller, name your field with trailing open-close square brackets like this: text_field_tag 'Name[]'. This makes Rails populate params['Name'] with an array of your input's values.
Note that :Name[] is not a valid Symbol in Ruby, so you'll have to use a String as the first argument to the text_field_tag helper.
I think you have a few things going on here:
Changing <%= text_field_tag :Name,(params[:Name]), size: 10 %><br> to <%= text_field_tag :Name, size: 10 %><br> might be the first step in moving forward.

optional date field with simple_form

I am using simple_form to generate date input like so:
= f.input :date_of_death
However, it always pre-populates with the current date.
How can I have it come up "blank" and allow the user to easily NOT enter a date?
Thanks!
There are a couple of ways to do this while still keeping the form split into dropdowns for Y/M/D.
= f.input :date_of_death, include_blank: true
Or if you want the first option to have text use prompt.
= f.input :date_of_death, prompt: "Choose Wisely"
Simplest way that I have found
f.input :date_of_death, as: :string
It will turn the input into a blank input field. You will have to parse the string as a date in your controller.
Use include_blank
= f.input :fecha_at, include_blank: true, start_year: Date.today.year - 90, end_year: Date.today.year

rails 3.2 update has_many :through

I'm pretty close on this one but caught up on a minor detail. I am trying to update a has_many :through relationship. When I submit the edit form I am unable to extract the appropriate attribute that I want to update. The loop that updates the qty shipped is not updated that field with the correct value. How can I extract only the qty_shipped attribute from the params[:product_shipments] hash?
This is the contents of my params[:product_shipments] hash that the update action is working with
"product_shipments"=> {"82"=>{"qty_shipped"=>"234"},
"83"=>{"qty_shipped"=>"324"},
"84"=>{"qty_shipped"=>"324"}},
"commit"=>"Update Shipment", "id"=>"250"}
Which has all the information I need to update the shipment because the #shipment.product_shipments loop limit the update to only the shipment_id applicable. My problem is that this is the following sql called by update action
ProductShipment Load (0.3ms) SELECT `product_shipments`.* FROM `product_shipments` WHERE `product_shipments`.`shipment_id` = 250
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 82
COMMIT
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 83
COMMIT
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 84
COMMIT
And here is the update action that produces the above sql:
def update
#shipment = Shipment.find(params[:id])
#shipment.update_attributes(params[:shipment])
#shipment.product_shipments.each do |shipment|
shipment.update_attributes(:qty_shipped=> params[:product_shipments])
end
respond_with #shipment, :location => shipments_url
end
Using rbates nested_forms gem is not preferred because I want to figure this out for the purposes of learning how rails works.
<%= hidden_field_tag("product_shipments[][#{product_shipment.id}]") %>
<%= hidden_field_tag("product_shipments[][product_id]", product_shipment.id) %>
<%= text_field_tag "product_shipments[][qty_shipped]", product_shipment.qty_shipped,:class => 'shipment_qty_field'%>&nbsp<%=#product.product_name %>
#shipment.product_shipments.each do |product_shipment|
product_shipment.update_attributes(:qty_shipped => params[:product_shipments][product_shipment.id][:qty_shipped])
end
You shouldn't have to do all this, just use Nested Forms. This is Rails!
http://railscasts.com/episodes/196-nested-model-form-part-1
Your params should look like this
{:product_shipments => { 79 => { :qty_shipped => 450 }, 80 => { :qty_shipped => 35 } }, :shipment_id => 1 }
To get that you should name your fields like this
<input name="product_shipments[79][qty_shipped]" value="450" />
<input name="product_shipments[80][qty_shipped]" value="35" />
To generate that,
<% #shipment.product_shipments.each do |product_shipment| %>
<%= text_field_tag "product_shipments[#{product_shipment.id}][qty_shipped]", product_shipment.qty_shipped || 0 %>
<% end %>

Iterate Form fields

I have build a module to add translations for each standard topic. Theses topic got many standard options and you can translate it directly in page.
I got an issue with my form about the edit view.
When i display a translation it's repeat all value of the f.input :value each time he have one and i want it to display with the each of standard value.
The question is how i can iterate my input field :value in the form to display only once per standard value and not repeat all value translated by standard value.
when i want create a new one all workings fine. It's just about the iterate field who is repeated how many times he got a field in the table.
the gist for my code :
https://gist.github.com/266562670cd8dab28548
Change:
<%= #preference_topic.preference_topic_options.each_with_index do |option, index| %>
<%= f.fields_for option.preference_topic_option_translations.first, option do |translate_form| %>
to:
<%= #preference_topic.preference_topic_options.each_with_index do |option, index| %>
<%= f.fields_for option.preference_topic_option_translations.first || option.preference_topic_option_translations.build, option do |translate_form| %>

Resources