Rails: select_tag - selected

i tried this here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
exactly this:
<%= collection_select(:sportlers, :sportlers_id, Sportler.all, :id, :name) %>
But there dont is written, how to get the selected value from the collection_select.
Im helpless, do you work with somethings like this?

I think collection_select will take the first two parameters, the object and the method, and use the values to make a hash, so in your case, you can access the values in your controllers like:
params[:sportlers][:sportlers_id]

Related

How to use `where` condition in options collections select?

How can I use where condition in options select? The following code is not working:
<%= options_from_collection_for_select(#group,:groupname, :groupname).where(User_id: #user.id)%>
Can anyone help on this?
options_from_collection_for_select takes a collection of records and options and returns a HTML string. So that of course won’t work as you are calling .where on a string.
You need to call it on the collection instead.
<%= options_from_collection_for_select(#group.where(User_id: #user.id), :groupname, :groupname) %>

Rails Checkbox format

I am trying to output checkbox with hard coded checkbox value, so far I have coded
<%= form.collection_check_boxes(:study_type,['Option1','Option2'], :first, :first)%>
The output for checkbox label is first alphabet of respective checkbox. Any way to display full text value label.
Thanks for help.
You have two ways for this:
First, convert your array into an array of arrays like this:
<%= form.collection_check_boxes(:study_type, [['Option1'],['Option2']], :first, :first)%>
Or, you can use the :to_s method like this:
<%= form.collection_check_boxes(:study_type, ['Option1','Option2'], :to_s, :to_s)%>
Both solutions are "hacky" and I personally wouldn't use it like that. A collection of objects is what's usually used in collection_check_boxes. More info here.

How to get each element of a Rails list into select_tag in a form?

I have a rails list like this :
["Ananda College", "Nalanda College"]
It is taken from a database. I have selected one column and taken all its entries. Now I need to put this list into a select tag! Select each one from the select box. How can I do this?
I tried to add a each do block inside the select_tag and it didn't work.
PS : The list is dynamically generated
You will need to use the form helper options_for_select which takes an array.
A common way of doing this is setting an array in the controller:
#colleges = College.uniq.pluck(:name)
Then in your view:
<%= f.select :college, options_for_select(#college) %>
Take a look at the docs for options_for_select for more implementation details.
try this it might be helpful
<%= f.collection_select :college_id, Collage.all, :id,:Name %>

Send an extra parameter through a form in rails 3

Is there a way to send an extra parameter through a form in rails 3?
For example:
<%= form_for #post do |f| %>
<%= f.hidden_field :extraparam, :value => "22" %>
<% end %>
but lets say :extraparam isn't part of the post model..
I have an unknown attribute error in the create method of the controller when I try this, any ideas?
(I want to use the param value itself in the controller for some extra logic)
Call hidden_field_tag directly. See: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag
These helpers exist for all the major form field types, and are handy when you need to go beyond your model's methods.
The following worked for me in passing extra parameters from the view back to the controller that were a part of my model and not part of my model.
<%= hidden_field_tag :extraparam, value %>
Example usage
<%= hidden_field_tag :name, "John Smith" %>
Ya Paul is right. Hidden_field is associated with your model whereas the extra _tag fields are not. I'm not sure of your needs but It's generally recommended in the RoR community to avoid passing a ton of hidden_fields like you might do in a php application.
Ive seen some code where ids were getting passed around in hidden fields which rails takes care on its own if you know the best practices and take full advantage of the framework. Of course I'm just saying this as general info as there are sometimes better ways at accomplishing the same functionality. Good luck on your apps.

Ruby on Rails Boolean Form

How can I pass a boolean value in a form to a controller without the user being able to see or edit it? I'm assuming hidden_field is used for this, but how can I then assign a value to the variable?
Thanks for the help
-Pat
Pat,
I am slightly confused by what you mean with the 'but how can I then assign a value to the variable', but I'll give this a go.
First off, you are correct in the hidden_field bit.
<%= hidden_field_tag 'some_name', true %>
or, alternatively
<%= hidden_field_tag 'some_name', false %>
You get the point with that, I'm sure.
From there, in your controller, when the form is submitted you would get the value of that field like so:
some_boolean = params[:some_name]
Obviously variable names would be different, but that's the general gist of it all.
Good luck!

Resources