collection_select does not reload the item after page reload - ruby-on-rails

i have the following code for collection_select
<%= f.collection_select :customer_id, Customer.all, :id, :name, options ={:prompt => "-Select a Customer"}, :style=>'width:210px;'%><br />
Problem :-
If i select a customer and then refresh the page, the collection_select does not refresh the list but shows the previously selected item.
How can i ensure that when i refresh/reload the page even the collection_select gets refreshed? guidance required (sorry for the bad English)

This is browser specific. Modern browsers can remember your choices upon a soft refresh and pre-select them. Generally a re-submission of the url should be enough to ensure a clean form is displayed.
Ctrl+F5, like Sachin asked you to do, generally removes the browser cache for that particular page before refreshing and hence works as you have seen.

Related

Rails Form: f.select for multiple options

For my user form, I have a hobbies drop-down menu and I want to be able to select more than one option (one user may have skiing, reading, and chess as hobbies).
Of course, doing this is very easy!
However, none of the available options seem to work for me...
Here's my code:
<%= f.select :hobbies, [['Chess','chess'],
['Movies','movies'],
['Videogames','videogames'],
['Skiing', 'skiing'],
['Reading','reading']],
{:multiple => true} %>
However, when I look at my form, I don't think this is working. It makes the drop-down menu....but how do I select multiple entries? I try ctr + click but it doesn't do anything....what am I missing? It keeps just selecting one value only...
Take a look at the accepted answer to that question - the method signature is:
select(:type, [data], {options hash}, {second options hash})
And in the answer, it has multiple: true in the second options hash.
API dock for select_tag gives a hint about what the two different hashes are for - it looks like the first options hash is for "option_tags", and the second one is for "options"

How to display selected value from the drop down menu in Rails?

I am a Rails newbie. I have created a drop down using collection_select and grouped_collection_select. My issue is that I am not able to display the selected option value from the drop down list. I have to display only the selected value on a new page but I am not able to get it.
Use the :selected => params[:name_of_form][:name_of_field]
= f.select :category,
options_for_select(['option', 'value']... etc ),
:selected => params[:user][:category]
I cant remember if the symbol goes on the inside like above, or if it
You should really be more clear when you are asking questions on SO, and you should also include code.
ALWAYS ALWAYS ALWAYS INCLUDE CODE!

Rails Populating SELECT options from database

I feel like this is basic but can't find details anywhere.
I have a basic application created by generating scaffold.
There is form already built into this that enters data. I have a SELECT (drop down) box in the form. I would like to be able to have the OPTIONS in this select box be pulled from a database. Ideally, the end user would be able to add and edit these options.
I have no idea how to link that SELECT field in my form to where the OPTIONS will be STORED.
Can someone point me in the right direction as far as terminology as what to research? I'm coming up blank. I feel like this must be a common thing to do..
As ByScripts' link includes, here is the script that worked for me (for those where time is not a luxury):
<%= collection_select(:lang_id, 0, Language.all, :id, :name) %>
Where Language is a table with one column called 'name' and an auto-assigned column id; :lang_id is the name of the element and 0 is the default selected index when the page loads.

Ruby on Rails: Entries per page

I have a search page that allows the user to search on many different fields. After clicking search the page displays 10 entries per page by default, with the option to view the next 10, or pick a page number, using will_paginate. There is a drop down menu that allows you to pick how many entries the user sees per page.
I have tried two pieces of code:
Results per page: <%= select_tag :per_page, options_for_select([10,20,50], :selected=>params[:per_page]), { :onchange => "this.form.submit();"} %></br>
and
Results per page: <%= select_tag :per_page, options_for_select([10,20,50], :selected=>params[:per_page]), :onchange => "'#{request.query_string}&per_page=' + this.getValue()" %></br>
When the first drop down is changed to 20 for example, the page instantly searches again, but displays the results 20 per page.
The second drop down allows you to select a entry per page number, but to change the view, you have to re click search.
I want my code to work more like the first drop down, but I think re searching is a bit expensive. Is there a way to stop resubmitting the search but change the display?
Thanks in advance
If you change the amount of items displayed per page, a requery has to be done anyway (because only the items to display have been loaded from the database). And if you change the items displayed per page, and you are on page 2, what will you see? Items 10 - 60? So if you change the items per page, you almost always also switch to page one, and that requires a requery anyway.
And second: it seems you want to reload the data in the javascript. You do a call to get the data, but seem to do nothing with the loaded data.

gmail-like popup checkbox list in rails?

I need to build something like what gmail does for it's labels... It has a button that when pressed pops up a scrolling list displaying the labels with checkboxes for selection.
I'd like to hear about approaches to do the popup and how to place it right under the button.
Also, I'd like to be able to observe the checkbox select/deselect events and take action, so advice on that part would also be appreciated... otherwise, I guess I'll have to put a form with a submit button and handle the new selections when the user submits.
If the checkbox list is static, you can do all this directly in the rendered action. Otherwise, two approaches are possible:
Use button_to_remote to retrieve an action displaying the popup and also serving the necessary js;
Use button_to_function to retrieve some XML or json (at your option) from an action, with the necessary labels and values for checkboxes, then render the popup.
The first may be easier to do if you're not familiar with all this, while the second is way more efficient, as only data is passed through the asynchronous call, and not markup nor javascript.
About your last question, if (un)checking the checkbox must result in a server side action, prototype_helper provides a convenient observe_field function, to be used like this:
<%= check_box "foo", "bar" %>
<%= observe_field "foo_bar", :url => {:action => :some_action, :controller => :some_controller} %>
If the (un)checking can be managed on client side, you can simply use:
<%= check_box "foo", "bar", { :onclick => "someFunctionToDoWhatINeed(someArg);"} %>
Just two notes:
JavascriptHelper and PrototypeHelper are just this, helpers: they allow you to do some things with a very simple syntax and are great, as long as they are helping; when they are no more, feel free to drop them and go for plain javascript.
I've used prototype for a while, but then I fell in love with jquery; you may want to take a look at it.
Please edit your question or comment my answer if I didn't understand your question and/or was unhelpful.

Resources