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.
Related
This is my first post, and I apologiese for repeating a previous question. But I'm new to Rails and need help from someone more experienced than I. I am trying to get a select box to select the database record.
My form reads:
I have tried adding
,params[:source]
And a few other methods which don't include the option for a select box. Could someone guide me as to what is missing to select my current record. Any info can be provided on request. I am using the current version of Rails on Mac OS. Thank you
As per the description mentioned in the post:
I am trying to get a select box to select the database record.
But you are passing statement values in options for select
<%= form.select(:source, options_for_select([['Thumbstack', 'Thumbstack'], ['Teacher', 'Teacher'], ['Google', 'Google'], ['Other', 'Other']])) %>
You need to change above one to the below mentioned:
<%= form.select(:source, options_for_select(ModelName.all.map {|h| [h.id, h.column_name]})) %>
Hope it helps!!
I am building a rails form and have an interesting problem I am trying to solve. I can't seem to find anything online to point me in the right direction. Thank you.
Is it possible to use a dropdown menu to select the :object_name for a text field?
In my head, I am picturing a collection_select form helper nested within a text_field form helper, though not sure this is possible.
In the form, I'd like the user to select the proper :object_name from an array
[:object_1, :object_2, :object_3, :object_4]
then give that entry a value with the text field
text_field(object_name, method, options = {})
The objects are all db columns in the same model.
Yes you can do that using jquery.
On change of the object names dropdown value, change the name attribute of the text field.
$('#selectObjectName').change(function(){
var field = document.getElementById("id-of-the-text-field-to-be-changed");
field.setAttribute("name", "value-came-from-the-selected-dropdown");
})
(Rail 5 beta 3)
I have a table on an index page (action) of a view with around 15 columns. Some of them are for text and some of them are for integers only. Every entry of this list (table) will be filled out by a 'form_for' form (in new or edit action).
For editing or deleting there are links with each list entry in the index view leading to the corresponding show, edit or destroy actions. This all works well. Some of these entries are entered by a select with pulldown on the new or edit view. This works well, too.
But if one of these selects should be changed for more than one entry in the list it takes too much time to click on 'edit', change the select and click on submit at each list item. To make this a better user experience I would like to be able to change the selects in the list (table) directly. It would be good to have the select/pulldown in place. The change of the state or choosen entry should than be saved in place as well or with an extra button ("save changes") above/below the table.
To say it in short:
I want to update multiple entries in a table in an index view without editig each single entry via edit view. The dates will be changed by a select and the data should be saved by a submit button on this page
Has anybody an idea how I can solve this?
Try best_in_place gem. It can solve the problem you have quoted. here are some links to it
https://github.com/bernat/best_in_place
http://railscasts.com/episodes/302-in-place-editing?view=asciicast
Your original text wasn't that confusing to me...
You want what's called a bulk edit feature. Easiest way would be to set up a new target at the controller level specifically to handle this request. I'll give you the pseudocode and it should be easy enough to fill in the blanks, but essentially:
Create a "bulk edit" form (the drop down select menu above the table)
Create a controller method to handle the bulk edit (controller#bulk)
Update routes.rb to direct a URL to that new method
Handle the bulk update in the controller and redirect back to index upon completion (cheap way of updating the page after editing is done).
Note: I'm assuming your model name is "Resource" because you did not specify what it actually was
On your index page, you want HTML like:
<form method="POST" action="/resources/bulk">
<select name="bulk_value">...</select>
</form>
On change/form submit/whatever, submit that form.
If you're using resourceful routing, you can hook this into config.rb via:
resources :resources do
post :bulk, on: :collection
end
Otherwise, hook the route however you see fit.
Then, in your controller:
def bulk
value = params[:bulk_value]
Resource.update_all value: value
redirect_to {resources_path}
end
Now, you said index view, so I am assuming you want all. But if you just want a subset, you can pass the preferred IDs along with the form as a hidden field, then use a where clause to filter, i.e.
Resource.where(id: parmas[:id_array]).update_all(value: value)
Anyway, that should get you down the right path.
I'm trying to add a simple select box and submit button to a "show" page in ActiveAdmin. Basically, the clients wants a simple way to assign a currently unassigned widget to the item currently being viewed. Not that that really matters.
What I am seeing is that although I can add a form and a select box, if I try to add anything after the select, the select doesn't get displayed. It's not that it is hidden by CSS, but that it just doesn't render.
Here's the relevant code:
column do
panel "Devices without locations" do
devices = Device.without_location
form_tag add_device_admin_location_path do
select_tag(:device_id, options_from_collection_for_select(devices, :id, :name))
submit_tag
end
end
end
The submit tag will be displayed, but the select will not. event if I put "foo" in there, only the "foo" will show up. The only time the select will show up is if there is nothing else in the block.
Update:
Okay, so I've been able to work around this by concatenating the output together. It's not ideal, and I definitely feel dirty, but it works.
I tried using formtastic on this, but it appears to only accepts attributes from the model, this doesn't work: I'm updating the device, not the location.
This works, but if anyone has a more better way of doing this, I'd love to know.
i had the same issue, and moving form into app/views/admin/#{model_name}s/_#{partial_name}.html.erb worked for me fine
I am making a photo upload form. Before they begin, they have two choices. They can create a new gallery, OR they can choose from an existing gallery.
I was thinking the best way to lay this out would be two have two forms. One for the create one that would take them to the create page. That's easy, and practically done.
But the second form, I need to grab the gallery name in a select box and apply it to an add_photos_to_gallery form - which is tricky for me. Do I?
a. Make a temp variable for this form and pass it to the controller, and then generate the form from what they choose? If so, how could I do that?
b. Use jquery and somehow manipulate the DOM to create a form? I got some ideas..
c. Realize I'm too dumb to know something way better and more obvious?
roughly speaking the way I would approach this is with one form,
to populate the select tag, i am assuming you create a named scope or association in your user model, eg
has_many :galleries
and populate the select tag with it
<%= select_tag "add_photos_to_gallery", #user.galleries, :include_blank => true %>
and if the params[:add_photos_to_gallery] is blank, create a new album otherwise add it to the selected gallery.
Or you could include a checkbox to toggle on or off the select tag based on if they want to use an existing gallery or not.