I have folowing schema:
Template
UserTemplate
template_id
Costs
template_id
amount
value
What I'm trying to do, is to create a sfWidgetFormDoctrineChoice that displays the shipping costs based on a UserTemplate id.
$this->widgetSchema['cost'] = new sfWidgetFormDoctrineChoice(array(
'model' => 'Costs',
'key_method' => 'getValue',
'method' => 'getAmount',
'add_empty' => 'Please Select Amount',
'expanded' => false,
'multiple' => false
));
This displays all of the Costs.
Ideally, I'd like it to limit it to the UserTemplate.
I have looked at creating a custom query and passing that into the widget, but I'm not sure if this would be the correct way of doing this
So If I have a bunch of costs assigned to the template id of 12 and the user template references 12, when I'm on example.com/user-template/12 - I'd expect to see the costs for this in my form widget.
Creating a custom query and passing it to the widget is exaclty what you're looking for. You will have to build the query depending on the template_id you use in the URL.
Related
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
} %>
I have the following code which allows a user to select a currency rather than inheriting one.
%dd= user_field(#user, :remuneration_currency, :type => :select, :collection => REMUNERATION_CURRENCIES)
I need to allow people to set this field back to NULL, which means that the user will pick up their account's default currency. However, if I change the code to this...
%dd= user_field(#user, :remuneration_currency, :type => :select, :collection => REMUNERATION_CURRENCIES, :blank => 'Company Default')
then it sets the value to blank (as it says on the tin) rather than NULL in the database. How do I set the 'Company Default' value to be NULL rather than blank.
Your best option is to use the gem nilify_blanks which does exactly what you are looking for:
In Rails when saving a model from a form and values are not provided
by the user, an empty string is recorded to the database instead of a
NULL as many would prefer (mixing blanks and NULLs can become
confusing). This plugin allows you to specify a list of attributes (or
exceptions from all the attributes) that will be converted to nil if
they are blank before a model is saved.
I am sorry if i cannot make you understand.
I have two tables where the first table contains categories and the next one contains sub-categories and i have another table item where i have has_and_belongs_to_many relation with sub-category table. When i create a new item, i have to display the categories in the first select box and based on the selection, the sub-categories has to be displayed in another select box and the value has to be added. I have already another like shown below
<%= collection_select :item, :sub_category_ids, SubCategory.find(:all, :conditions => ["category_id = 7"], :order => 'sub_category_name ASC'), :id, :sub_category_name, { :selected => #item.sub_category_ids }, { :multiple => true, :name => 'item[sub_category_ids][]' } -%>
and now i want to add this one like
along with that. How can i do this?
You'd have to use AJAX to achieve what you want.
You would want to use Javascript to watch the first drop down value. When that value changes, you would want to make an ajax call to retrieve the values of the second drop down, then fill the second dropbox based on the AJAX response
I want to create a form for making a reservation for borrowed items. A reservation consists of pick up time, return time and items the reserver wants to borrow and their amounts. For example, I might want to reserve three plates, three knives and three forks for the rest of the week.
In the form, I want to do an AJAX validation that checks whether there is enough items available. Next to each item I have a text box with which the reserver inputs the item amount. After the input, I want to do an onchange call that checks if the amount of items is available for the given dates. Thus, I need to pass the remote function called in the onchange following parameters: item id, item amount (value of the current textfield) and pick up time and return time which are both given in datetime_select fields above. This is my code:
<% with = "'amount='+value+'&item=#{item.id.to_s}&pick_up_time=#{#reservation.pick_up_time.to_s}&return_time=#{#reservation.return_time.to_s}'" %>
<%= text_field_tag "reservation[#{prefix}reserved_items][#{item.id}]", get_amount_value(item, #reservation), :size => 3, :onchange => "#{remote_function(:url => { :controller => :items, :action => :availability }, :with => with, :update => availability_url) }" %>
Obviously, this does not work since #reservation.return_time and #reservation.pick_up_time are not yet set because the form is not yet sent. My question is: how do I get those values? I believe that it should be done via a javascript call, but I didn't manage to insert a javascript call in the "with" variable or at least didn't get it to work. Does anybody have any idea on what I should do?
use prototype selectors $(#reservations_pick_up_time).value
the :with attribute is just a place for you to write JS code that it will display inline
I have a partial that renders a select box using the following method:
<%= collection_select 'type', 'id', #types, "id", "name",
{:prompt => true},
{:onchange =>
remote_function(
:loading => "Form.Element.disable('go_button')",
:url => '/sfc/criteria/services',
:with => "'type_id=' + encodeURIComponent(value) + '&use_wizard=#{use_wizard}'"),
:class => "hosp_select_buttons"
} %>
This partial gets used 2 times on every page, but at one point I need to get the value of the first select box. Using:
$('type_id')
returns the second select box. Is there a way to find the first one easily? Should I fix this using javascript or by redoing my partial?
Note: the dropdowns do get rendered in separate forms.
Yes, each element does need a unique ID, the page probably also fails HTML validation. Also, unless these are in 2 different forms you'll have a conflict with the CGI parameter name.
If they are in 2 different forms you can probably get away with just setting the :id as you posted, if they are the same form you need to abstract the parameter name too:
collection_select 'type', "id_wizard_#{use_wizard}"...
I figured out one way to do this by assigning an ID in the html_options block. I already am passing in a value for use_wizard, so I can append that value on to the ID to differentiate between the two dropdowns.
:id => "type_id_wizard_#{use_wizard}"