options_from_collection_for_select Definition - ruby-on-rails-3

I have just read the Rails API definition for option_from_collection_for_select.
Returns a string of option tags that have been compiled by iterating over the collection and assigning the result of a call to the value_method as the option value and the text_method as the option text
Now I am very new to rails so was wondering if someone could break this down into smaller chunks and explain what is happening, dumb it down if you will, the explanation is very high level (well for me at the moment)
Thank you

Using the example from the Ruby on Rails API, let's assume you have a Person model that has an id attribute and a name attribute.
Say you have a form that creates a new project and assigns it to a person. Maybe you want to have a drop down select for what person you're assigning this project to. You could use options_from_collection_for_select for something like this.
<%= f.label :person, "Assigned Person" %>
<%= f.select(:person, options_from_collection_for_select(#people, "id", "name") )
(f by the way would be referring to the #project variable for our example form here.)
What this would do is create an option in your select drop-down for each person contained in the instance variable #people. Each of the <option> tags would have the id of that person assigned to its value attribute, and the text for that option would be the person's name.
So if your #people variable contained [#<Person id: 1, name: "Brock Sampson">, #<Person id: 2, name: "Byron Orpheus">], you would get HTML output like this:
<label for="project_person">Assigned person"</label>
<select id="project_person" name="project[person]">
<option value="1">Brock Sampson</option>
<option value="2">Byron Orpheus</option>
</select>
Does that make more sense?

Related

How can I set the checked property using vuelidate from an array of options?

I have an array of activities that looks like this:
[
{id: 'd0a503f0-be2b-453c-8f6e-d5a734bbba33, name: 'Baseball'}
{id: 'd0a503f0..., name: 'Soccer'}
...
]
I am looping through each activity to render a list of checkbox options like this:
<div v-for="activity in activities" :key="activity.id">
<input :id="`activity-${activity.id}`"
v-model="$v.resource.activities.$model" name="activities" :value="activity.id" />
<label>{{ activity.name }}</label>
</div>
So far, things work great. The checkbox group renders with the correct value(s). I am able to check the ones I want and when I save my form, I am saving an array of activity id's.
When I edit the resource, the checkboxes are not being pre-populated (checked). I am struggling to understand how to loop through each of the array elements in vuelidate's $v.model.
Let's say I have 4 activities total. Here is what the $v.resource.activities attribute looks like in my devtools toolbar (2 activities have been previously selected):
$v:Object
resource
activities:Object
$anyDirty:false
$anyError:false
$dirty:false
$error:false
$invalid:false
$model:Array[2]
0:Object
createdAt:"2023-02-04T08:55:27.009Z"
id:"d0a503f0-be2b-453c-8f6e-d5a734bbba33"
name:"Baseball"
updatedAt:"2023-02-05T23:17:00.206Z"
1:Object
createdAt:"2023-02-05T23:19:49.938Z"
id:"c251e931-2a8a-468c-aff4-b4250233cfa6"
name:"Soccer"
updatedAt:"2023-02-05T23:19:49.938Z"
I know I need to keep the initial loop like I have above to generate the list of checkboxes. I am confused as to how to also loop through each of the $model attributes to see if the checkbox should be checked or not.
Since the value of the checkboxes are only the id of the activity, I don't think it knows what to do (which is understandable). How can I loop through the list of activities and correctly populate which checkbox has been selected?

How to get value of checkbox in capybara?

i have code html like this
<input type="checkbox" class="class-of-checkbox" value="facebook">
how to get the value (facebook) ?
You first need to uniquely find the element and then call value on it. How you find the element uniquely can be highly dependent on the structure of the rest of the page, but based on just the single element HTML you've provided (and you not actually knowing what the value already is) either of the following could be a starting place
find('.class-of-checkbox').value
find_field(type: 'checkbox', class: 'class-of-checkbox').value
If you just want to verify the value of an element with an expectation then you could do (assuming RSpec with cucumber)
expect(page).to have_field(type: 'checkbox', with: 'facebook', class: 'class-of-checkbox') # you can also pass :checked option if you want to verify it is checked - or use have_checked_field/have_unchecked_field

Can I add the custom fields to the product listing page in BigCommerce

Each product has the custom fields options. Can I output those custom fields on each product item in the product list page? If so, how? I have tried adding the ProductOtherDetails and the %%SNIPPET_ProductCustomFieldItem%% in the CategoryProductsItem.html, but got no output at all of any of the items I have tried. Any suggestions or pointers on how and if this is possible?
As of September 2015, you can now access %%GLOBAL_ProductCustomFields%% on any template file that renders a particular panel's individual items. For example:
-Snippets/CategoryProductsItem.html for category list pages
-Snippets/HomeFeaturedProductsItem.html for the featured products panel
I recommend adding the custom field name as a class to each field for easy hiding, and accessing of the value in case the custom fields ever change you won't be accessing them via :nth-child CSS which would break. You can do so by modify Snippets/ProductCustomFieldItem.html to add the custom field name to the CSS class or ID like this:
<div class="DetailRow %%GLOBAL_CustomFieldName%%">
<div class="Label">%%GLOBAL_CustomFieldName%%:</div>
<div class="Value">
%%GLOBAL_CustomFieldValue%%
</div>
</div>​
Doing so, will output like this in each item in the category list.
Above, I am using the custom fields to send through shipping time, as well as "Starting At" to prepend to the list page price if the item is a parent which has children of higher prices. In my opinion, these features greatly increase the user experience.
For Faceted Search (handlebars.js)
I recommend adding this to Panels/FacetedSearchProductGrid.html:
{{#each product.custom_fields}}
{{ id }} : {{ name }} : {{ value }}
{{/each}}
Those filters will be limited to the Product pages specifically. The only way around it is to hash together a solution using jQuery to go and fetch items in question from the product page. A pain, but do-able with unnecessary effort.

Checkboxes using struts

Hi I am willing to update the value of the checkbox , fetched from the Database, loaded on the JSP. I am creating a Employee Profile. The Jsp has fields Employee Name, Employee Address, Employee technical Skills.
The Employee Skills has following Checkboxes to select the following values
Checkbox1: Java Checkbox2: Dot net
For a Employee X, Does not have both the computational skills Java and Dot net in the database. I am able to fetch the record from the database to the JSP.
In the action class I am fetching the values from the database and setting them into the EmployeeForm which has the two getters and setters. I am setting these values in request also with setattribute.
In the EmployeeUpdate.jsp I am doing the following
String skillsValue1=(String)request.getAttribute("C/C++");
String skillsValue2=(String)request.getAttribute("Java");
<%if(skillsValue1!=null){ %>
<html:checkbox property="ComputationalSkill1" value="C/C++"/>
<%}else{ %>
<html:checkbox property="ComputationalSkill1" value=" "/>
<%}%>
</td>
<%if(skillsValue2!=null){ %>
<html:checkbox property="ComputationalSkill2" value="Java"/>
<%}else{ %>
<html:checkbox property="ComputationalSkill2" value=" "/>
<%}%>
</td>
The following code displays the checkboxes perfectly for the employees with any of the above skills and without skills.
I am getting the following problem here
When I uncheck the value on any field, which was checked earlier , the value is setting to null in the bean perfectly.
But when When I Check the value on any field, which was not checked earlier(to update any of the fields), There is no value set in the bean for the particular property, neither null also.
Could anyone let me know how to get the changed values of a checkbox, i,e when I change the state from uncheck to check state, the value should be set in the property.
If an employee has the skill Java, you're displaying the following checkbox:
<html:checkbox property="ComputationalSkill2" value="Java"/>
This means that on submit, if the checkbox is checked, the following parameter will be sent:
ComputationalSkill2=Java
If an employee doesn't have the skill Java, you're displaying the following checkbox:
<html:checkbox property="ComputationalSkill2" value=" "/>
This means that on submit, if the checkbox is checked, the following parameter will be sent:
ComputationalSkill2=<blank space>
What you want is generate always the same checkbox (the first one), but preselect it if the employee has the skill Java, and not preselect it if the employee doesn't have the skill Java.
This is not possible as is using the <html:checkbox> tag, because it's supposed to be bound to a boolean property. So you could instead have a property isJavaSkilled()/setJavaSkilled() in your form bean, and use
<html:checkbox property="javaSkilled"/>
Struts will pre-select the checkbox if the form's javaSkilled property is true, and leave it unchecked if it's false.
Note however, that since you have a list of skills that can be or not present, you should instead have a property getSkills()/setSkills() of type String[], and use a <html:multibox> tag.

Rails 3 Select box posts a blank variable but the HTML is well-formed

I'm having problems with the select tag in my view.
First of all I'm using Rails 3.1.1 and Ruby 1.9.2 p290
The model is a Consumable which has a name, supplier, reorder number, type and associated printer models that it is compatible with.
The field that isn't validating correctly is type which is fed values from an array in the Consumable model and the validation should be against inclusion of one of the elements of the array.
In the model, I define the field's validation like so
TYPES = [ "REM", "REM-HICAP", "OEM", "OEM-HICAP" ]
validates :type, inclusion: TYPES
In the view (form partial for Consumable) I've created the select tag thusly
<div class="field">
<%= f.label "Type" %><br />
<%= f.select :type, Consumable::TYPES, prompt: 'Select a type...',
:selected => params[:type] %>
</div>
The drop-down box is creating well-formed HTML which looks like this
<div class="field">
<label for="consumable_Type">Type</label><br />
<select id="consumable_type" name="consumable[type]">
<option value="">Select a type...</option>
<option value="REM">REM</option>
<option value="REM-HICAP">REM-HICAP</option>
<option value="OEM">OEM</option>
<option value="OEM-HICAP">OEM-HICAP</option>
</select>
</div>
When I post the form this is the debug information
---
utf8: ?
_method: put
authenticity_token: nZZ9MastYswVCDrvAbgVLTUWqSRZLVrvRLmxOqPYk7I=
consumable:
description: Black Toner
supplier_id: '1'
reorder_no: '123456'
type: OEM
printer_model_ids:
- '1'
- '3'
commit: Update Consumable
action: update
controller: consumables
id: '1'
As is evident by the debug info, type is set correctly with a value from the select box (a string) and type in the model is a string. The form instead shows the validation error "Type is not included in the list" and if I remove the validation the database shows a nil value for the type field.
At what point is the validation applied to the form, does the controller trigger it by attempting to save the object?
I have used the same method of validating a drop-down box in the depot application in Agile Web Development with Rails 4th Edition with essentially the same code but a different name for the array and it works correctly.
I have just tested the same validation in the depot application and it works on this same workstation, so I would count out setup inconsistencies.
Thanks
I finally discovered my error: Type is protected in Rails and changing my variable name to Kind make it work correctly.