how to add a field related on odoo
I would like to add a text field on stock.move related to stock.picking origin field
I tried this :
_columns={
'sourcebl': fields.related('picking_id', 'origin', type='char', relation='stock.picking', string='Class Description', store=True, readonly=True),
}
do you have any Idea
You can find more information here : Link
In the new API, there is not anymore fields.related. You should define your related field like this :
sourcebl = fields.Char(string='Class Description', related='picking_id.origin')
I used the old api it work fine now :
_columns={
'sourcebon': fields.related('picking_id', 'origin', string="Origin", type="char",store=True, readonly=True),
}
Related
I'm creating a registration form where a user inputs there address details. For the country field, i would like it to be defaulted to "united kingdom', in the database unless specified other wise, but i would like the form input to be blank. At the moment, it currently show "united kingdom' in the form. is it possible to do this?
I've already done the migrations for it. thanks
Per the docs here: https://apidock.com/rails/ActionView/Helpers/FormHelper/text_field
You can specify a value for the field:
<%= text_field :your_model, :country, value: '' %>
Hard to say exactly what you need since you haven't provided any code, but you could add validation that sets the value to 'United Kingdom' if the value of that field is blank?
I have a form in my ActiveAdmin model and I need to translate the content of the button that is generated to upload a file.
I have the following form:
form do |f|
f.inputs 'Details' do
f.input :orders_file, as: :file
end
actions
end
Which displays this:
I would like to translate or change that 'Choose file' and 'No file chosen' texts.
I've tried with
input_html: { title: 'this', text: 'will', label: 'work', value: 'please' }
but no luck.
Thank you!
Changing the file upload field labels involves some special techniques invented by Michael McGrady, discussed in detail at https://www.quirksmode.org/dom/inputfile.html
You can not change the file field default labels on buttons and text, they are hard-coded in browsers. Each Browser has its own way to render these things.
Only way to change these labels is, build your own custom file field control.
This shouldn't be a problem but I can't figure it out.
Using form helpers I populate a select field like so:
...
<%= f.select(:entity_id, entities.map {|entity| [entity.name, entity.id]}) %>
...
This is to create a User and set their entity_id. After the form submission I now have a new User whose entity_id is now the value of the option from the select field.
For example if I had selected "Store A" as my entity from the select element, the entity_id for that user is now '1' after I submit the form. This is great but I also need the entity_name "Store A" associated with the user.
So instead of my new user returning the following:
#<User name: 'John Doe', entity_id: '1', entity_name: 'Store A'>
I am getting
#<User name: 'John Doe', entity_id: '1', entity_name: nil>
Which makes sense becuase I never set entity_name.
How would I set entity_name in this form based on the selection ?
If I understand your question correctly, it looks like you want to set two values at the same time for a single input box. Rails does not provide a simple way to do this but it seems that this answer is similar to what you want.
However, I'd still ask the question: Why try to do it all on form submission?
You are already setting the entity_id on the User so you have a few options.
Assuming that entities is a relation of ActiveRecord or some other ORM objects, you could let the association do the work. If a user has_one :entity and then needs to know its entity_name then it could look like: user.entity.name after you set the correct entity_id on said user. This leverages the built in relationship framework Rails provides and keeps you from duplicating data.
If entities is not a relation or set of persisted objects, the code you used to generate the form must have known about entities during render time so it stands to reason that you would have access to it again. In the controller that accepts this form, fetch entities the same way you have done previously and link the entity with the submitted entity_id's name to the User you are updating.
If neither of these are feasible or make sense, go ahead and use the JSON encoded form values.
Try this
<%= f.select :entity_id,
options_from_collection_for_select(#entities, :id, :name, entities),
{},
{
data: { placeholder: "Choose entity..." },
multiple: true
} %>
First time I try to customize rails_admin and by default I am getting text field which I want to convert in textarea. In model I have given datatype as string So is that possible to display textarea?
config/initializer/rails_admin.rb
config.model Product do
list do
exclude_fields :id, :created_at, :updated_at
end
create do
......
configure :description do
partial 'my_partial_file' # to override field I have created partial file
end
.....
end
end
views/rails_admin/main/_my_partial_file.html.haml
= form.send field.view_helper, field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), class: 'form-control', required: field.required})
I tried html_attributes rows: 50, cols: 60 also tried to apply custom_css but doesn't help. Please guide me where I do mistake? And if possible please make me understand syntax of this _my_partial_file
Edit:
If I do something like this then I can get textarea
field :description, :text do # use second parameter to set field type
required true
#partial 'my_partial_file'
end
But if I render partial then again text_field shown. :( I want text_area + partial file should also rendered as it contain other code to process.
I have solved this issue by my own. I thought to delete this question but then realized if in future anyone faced same issue then my solution can be helped.
What I had changed in _my_partial_file is:
= form.text_area field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), class: 'form-control', required: field.required})
........ # other piece of code
and in config/initializer/rails_admin.rb I have keep the code as it is:
create do
......
configure :description do
partial 'my_partial_file'
end
.....
end
I got the reference from here: https://www.omniref.com/ruby/gems/obitum-rails_admin/0.0.5/files/app/views/rails_admin/main/_form_text.html.haml#line=5 (Wayback link)
and
http://ruby-doc.org/gems/docs/r/rails_admin_settings-0.8.0/app/views/rails_admin/main/_setting_value_html_haml.html
(Note: link dead. This may be the same file from that older version: https://github.com/rails-admin/rails_admin_settings/blob/v0.8.0/app/views/rails_admin/main/_setting_value.html.haml )
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