Empty default value for select year - ruby-on-rails

I'm working on a form using ruby on rails and I have a select tag to choose a class year
=select :student, :class_year, (Time.zone.now.year - 30)..(Time.zone.now.year + 10)
This gives me the default value the current year - 30. I tried to add an empty default value but I get errors and I can't figure out how to get it to work. I'm very new to ruby.
I would appreciate any help.

It looks like the problem is that you're using a Range as the third parameter for the select method. As stated in the Rails API documentation, this choices parameter needs to be either a flat collection (see options_for_select) or a nested collection (see grouped_options_for_select).
In your case, rather than (Time.zone.now.year - 30)..(Time.zone.now.year + 10), which results in a Range, you could use the following to produce an array of strings:
((Time.zone.now.year - 30)..(Time.zone.now.year + 10)).to_a.map(&:to_s)
I suggest you take a look at https://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease for some excellent guidance about how to use the select form options helper.
I hope this helps!

Related

Add types in the parameter table when generating document with Stardoc

We are trying to use Stardoc when generating documentation for our Bazel macros.
I can get a table generated with parameter descriptions when follow the instruction here:
Macro/Function documenatation.
But we would also like to add the parameter Type in the table.
According to DocstringUtils.java it seems like we shall write the type in parentheses after the parameter name but before colon, as this example:
another_parameter (unused, mutable): a parameter may be followed
by additional attributes in parenthese
I have seen that it's possible to add a rule template to the stardoc() rule (attribute: func_template).
I started to use an own copy of the default template to play around with: //stardoc:templates/markdown_tables/func.vm
If I have understood correctly it doesn't seem that the attributes that I add in paranthese is fetched.
And I don't think that I will be able to retreive that information by just updating the template.
So I think that it will need an update in the Stardoc code for this, correct?!
Is that something that is already planned?
If I'm not correct then I would appreciate information how I can retreive the value of the attribute.
Thanks!
Best Regards
Elisabeth

Split datetime value received from external API in Rails app

I have a datetime value which comes from the API in this format: 2015-07-07T17:30:00+00:00. I simply want to split it up between the date and time values at this point. I am not using an Active Record model and I prefer not to use an sql database if I can.
The way I have set up the app means that the value is "stored" like this in my view: #search.dining_date_and_time
I have tried two approaches to solving this problem:
Manually based on this previous stackoverflow question from 2012: Using multiple input fields for one attribute - but the error I get is the attribute is "nil" even though I put a "try"
Using this gem, https://github.com/ccallebs/split_date_time which is a bit more recent and seems to be a more elegant solution, but after closely following the doc, I get this error, saying my Search model is not initalized and there is no method: undefined method dining_date' for #<Search not initialized>
This is when instead I put #search.dining_date in the view, which seems to be the equivalent of the doc's example (its not that clear). The doc also says the method will be automatically generated.
Do I need to alter my model so I receive the data from the API in another way? ie. not get the variable back as #search.dining_date_and_time from the Search model for any of this to work?
Do I need an Active Record model so that before_filter or before_save logic works - so i can (re)concatenate after splitting so the data is sent back to the API in a format it understands. Can I avoid this - it seems a bit of overkill to restructure the whole app and put in a full database just so I can split and join date/time as needed.
Happy to provide further details, code snippets if required.
As I am not using a conventional Rails DB like MySql Lite or Postgresql, I found that the best solution to the problem was by using this jQuery date Format plugin: https://github.com/phstc/jquery-dateFormat to split the date and time values for display when I get the data back from the API.
The Github docs were not too expansive, but once I put the simply put the library file in my Rails javascript assets folder, I just had to write a few lines of jQuery to get the result and format I wanted:
$(function() {
var rawDateTime = $('#searchDiningDateTime').html();
// console.log(rawDateTime);
var cleanDate = $.format.date(rawDateTime, "ddd, dd/MM/yyyy");
// console.log(cleanDate);
$('#searchDiningDateTime').html(cleanDate);
var cleanTime = $.format.date(rawDateTime, "HH:mm");
// console.log(cleanTime);
$('#searchTime').html(cleanTime);
});
Next challenge: rejoin the values on submit, so the API can read the data by sending/receiving a valid request/response. (The values can't be split like this when sent to the remote service).

auto_complete_for: prevent the first item from being auto-selected

The auto_complete_for dealio from script.aculo.us is great an all, but is there a way for me to selectively disable the fact that it always auto-selects the first item in the list?
The problem is that, if I want to type my own entry that is new, and novel, I don't want the first item in the list to be auto-selected. The reason is because when I TAB out of the field, it selects, and fills the text box with that first item.
I got around that, somewhat, by making the first item in the list the same as what I'm typing, but that's not perfect either, because the auto_complete list doesn't always update with every keystroke, depending on how fast I type. I've tried setting the list refresh rate to the lowest value (1 millisecond) but no go.
What I really want is an option in "auto_complete_for" that doesn't select that first item at all - the same way that Google Instant doesn't automatically select the first suggested search phrase - you have to arrow-down to select one.
Maybe I can do this via an HTML option that I'm missing?
Looking at the source, there doesn't appear to be an option for that, but I bet if you changed line 284 of controls.js to this.index = -1; it would do what you want.
Otherwise, it might be time to look for a different autocomplete widget.
If your requirements are too far away from the available plugin, then I guess there is no point in tinkering around. Its best to write your own JS code.
You might want to consider this: https://github.com/laktek/jQuery-Smart-Auto-Complete
or this : https://github.com/reinh/jquery-autocomplete
I'll add another alternative that works great with Rails 3:
http://github.com/crowdint/rails3-jquery-autocomplete
I recently implemented auto complete for more than a field for Rails 2.0.2.
The plugin I used is:- https://github.com/david-kerins/auto_complete . Not sure if it supports Rails 3.
I have also encountered issues on implementing the above scenario and have posted questions( Implementing auto complete for more than one field in Rails ; Implementing a OnClick kind of functionality and formatting wrt Rails Partial-Views ) on stackoverflow for the same, I have been lucky on getting things working for me based on my requirement.
Kindly refer to these questions, they might have relevance to your requirement.

How to "order by" a sfWidgetFormDoctrineChoice in the Admin Generator

I'm using Symfony 1.4 and Doctrine.
Let's say I have 2 classes : a Brand and a Product.
When I create a new product in the Admin Generator based admin, I'd like to choose a brand from a dropdown list.
The Admin Generator is doing that for me, automatically creating a sfWidgetFormDoctrineChoice.
The problem is that the brands are ordered by id. I'd like them to be ordered by their "label" field.
In order to do that I did the following in my ProductForm class:
$this->widgetSchema['brand_id']->addOption('order_by','label');
But I get the following error:
Syntax error or access violation: 1064
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'a' at line
1. Failing Query: "SELECT b.id AS b__id, b.external_id AS
b__external_id, b.label AS b__label,
b.created_at AS b__created_at,
b.updated_at AS b__updated_at FROM
brand b ORDER BY l a"
The order by statement is really weird. I don't understand why it seems to cut the name of the order by statement.
Edit: Apparently the 'order_by' option is expecting an array as a second parameter. What values does it expect?
I didn't try benlumley's solution since he answered right when I found my solution. It seems more tedious than what I ended doing.
I took a look at the source code to figure out how all this was working.
It turns out the "order_by" option needs an array specifying the field on which one wants to order the results and either 'asc' or 'desc':
$this->widgetSchema['product_id']->addOption('order_by',array('label','asc'));
It works like a charm.
You should take a look here:
http://trac.symfony-project.org/wiki/HowtoSortAdminGeneratorListByForeignTableName
Its based off of an old version of symfony, so suspect the plugin it links won't work. But I think the method should still be sound - crux of it is that you have to add method to the action to intercept and amend the default handling of sorting by this specific field:
For doctrine, you need to define/override addSortQuery, for propel, addSortCriteria.
Recommend you take a look in the cache folder to see what the auto-generated classes look like to get the hang of how it works.

Changing the order of params in pagination links

I am using Cakephp to perform a search through sphinx. I wanted to do modify the default structre of pagination links generated by cakephp
For example
From:
localhost/search/page:1/key1:google/key2:code
To:
localhost/search/key1:google/key2:code/page:1
I want the page number to appear at the end. Is there a way this can be done?
Any help appreciated
I would suggest that you modify the PaginatorHelper. I would recommend that you extend it and load it when Cake launches so that you dont modify the Cake file in cake/libs...
Then, wherever you want the output URL to be printed w/ the page number at the end, you'll need to modify the PaginatorHelper... I'd suggest you search for the page/# key/value pair. If you find it, remove it, then append it to the end of the string. Then return that value.
Edit: link to PaginatorHelper in the API - http://api.cakephp.org/class/paginator-helper

Resources