Hoping this will be a straight forward question, but is anyone able to let me know the best way of populating a hidden field based on a value on another table.
I currently have 2 tables - table_numbers which has the following fields - id and value(decimal), and table_records which has an amount field.
On the form to add a new record I have the following to add a value to it
= f.association :table_number, :collection => table_numbers.order('value ASC'), :label_method => :value, :prompt => "Select a value", :label => "value"
At the moment this is populating the number_id on the records table, but displaying the value on the form when adding a record. What I would like is to get the value as well to be able to run a calculation on the value and amount.
What would be the best way to do this? Update the line above or do I need to add extra code?
Thanks
You can perfom calucaltions in before_save callback in model. If you want to display calculations, use helper and show it.
Related
Is it possible to make select_tag return a pair of values (group_id, item_id) using option_groups_from_collection_for_select?
= select_tag :item_id,
option_groups_from_collection_for_select(#groups, :items, :name, :id, :proper_name),
class: 'form-control'
Each group has many items association, each item can belong to one or more groups.
I'd like to make the helper to add group_id to optgroup id attribute in addition to adding item_id to each item option and fetch those two on selection to send them to a controller, but I can't figure out how, nor is it even possible (or proper..).
I could make two different dropdowns, where 2. one would be loaded using ajax after selecting an option in the 1. one, but this just doesn't seem right..
EDIT
I've figured that I can pass a proc as a value_method, but unfortunately I'm unable to fetch group_id in there..
= select_tag :item_id,
option_groups_from_collection_for_select(#groups, :items, :name, ->(el) { "#{el.id}" }, :proper_name),
class: 'form-control'
Kind regards!
EDIT
Ok, I've finally figured it out, thanks to https://stackoverflow.com/a/6374301/19174916 :)
Ok, I've finally figured it out, thanks to https://stackoverflow.com/a/6374301/19174916 :)
Used grouped_options_for_select and passed group_id as a data attribute to each option, which I'd pass along with url later using simple JS.
I want to send two values with my collection select. Currently, i'm saving the ID but I want to have access to the name in my controller as well.
= f.collection_select :foo_id, #foos, :id, :name
That's my code, pretty simple. Just having trouble getting access to that name.
The collection also comes from a external API, so I don't want to have to touch the API again to get the name.
As, I said in the comment. You can customize the <option> tag values like :
= f.collection_select :foo_id, #foos, ->(ob) { "#{ob.name}|#{ob.id}" }, :name
Now, inside the controller just split it the value on |, and use it.
This is just an idea out of millions.
I want to override an attribute in my Rails Project but the problem is, that in my form (Formtastic) it still displays the database value and not the one from the getter. Do I have to write a custom getter and setter with annother name and save the value to the first attribute or is there a better way to do this?
If I understand you correctly you want to have a field in your form with a specific value, that is different from the actual value in the database. In this case you could pass the value option like this:
f.input :field, :input_html => { :value => "value" }
I am trying to learn by doing and need some help.
So formtastic can handle belongs_to associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model.
What I am trying to do is create a post without having to select the author in the create form.
I want to set the author id via a hidden form field but when I try this
<%= f.input :author, :as => :hidden, :value => '33' %>
I get uninitialized constant author and tried various other ways that Ive failed to get to work.
The closest Ive got has been by trying to customise the choices for the select and hiding the select as follows:
<%= f.input :author, :as => :select, :collection => ["33"], :input_html => { :style => 'visibility: hidden' } %>
But even though the above hides the select and works, it still displays a form label for author which I dont want it to display
Here is an example to better explain my scenario if what Im trying to achieve is still unclear:
Imagine you have a list of authors page and as an author you click on your name to get into an area where you can manage various things and create posts.
The problem: When you click create a post and the form renders, you dont want to have to select your name from one of the fields to tell the app that the post you are creating belongs to you because you want the system to know its yours because you are in a config area that belongs to you.
I am trying to do this by using a hidden form field to set the author id so that the post is saved with the correct association when the user submits the form.
Can anyone provide me with an example of how to do this? and as Im a learner maybe there is a better way to approach this so can anyone please advise and point me to some good examples
You should not be populating it in a hidden form field; hidden form fields can still be changed by the user and in the end you are allowing users to create posts by other authors.
The correct way to do this would be to assign #post.author = current_user in the create action of your PostsController.
I'm developing project on rails 2.3.8 and I need to observe field on drop down menu which develop using collection select. Please can some one explain how to observe field ?
My collection select code is like this
<%= collection_select("event", "trainer_id", #trainers , :id, :name, {:prompt => true}) %>
And I don't know how to use observe field for this. So please can some one explain about this ?
Related: Auto populate a text field based on another text field
observe_field(field_id, options = {})
Observes the field with the DOM ID specified by field_id and calls a callback when its contents have changed. The default callback is an Ajax call. By default the value of the observed field is sent as a parameter with the Ajax call.
Read this details: http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/observe_field