Open marccantwell opened 13 years ago
Yeah, so selects are a bit of a pain... the settings for the field are basically being passed directly to the jeditable plugin - check out the "How to use Selects?" section on its documentation page. jeditable expects a hash of keys and values, so currently an array needs to be converted. I have a helper in my application that creates this (see here) - this is what it would look like for your example:
def states_json_string
# assuming State::NAMES => ['Alabama', 'Arkansas', ...]
states_pairs = ["'': 'Select a state...'"]
states_pairs.concat State::NAMES.collect{|s| "\"#{s}\": \"#{s}\""}
"{#{states_pairs.join(', ')}}"
end
Then, the view code would look something like this (adapted from here):
editable_field @user, :state, :type => 'select', :data => states_json_string
This is something else that needs to handled by the gem. On the to-do list...
Are you still working on this gem? We'd really benefit from the ability to edit selects as easy as text fields. Also, I don't understand how State::NAMES would have to be set up :(
Sorry bout that - been meaning to come back and overhaul this gem for a while, but keeps getting pushed back on my to-do list. Will try to get to it soon - thanks for the patience.
No unnecessary hurry, we'll be developing that app for some more months ;)
If you could explain how you set up the State::Names thingie, that might already help to at least get the make shift running.
Hi! I have been using your gem (which is awesome by the way! thank you very much for making it), and encounter the same issue. I want to share with you how I solved it in case you might find it useful.
In your gem file include 'json' like the following:
gem 'json'
This is a gem that helps you convert any hash into a json string.
def json_hospitals(hospitals)
hashHospitals = Hash.new
hospitals.each do |hospital|
hashHospitals[hospital.id] = hospital.name
end
return JSON.generate(hashHospitals)
end
This will grab all the hospitals that are in your database and put them in a hash. The hash is then converted and returned as a json string using: JSON.generate(nameOfYourHash)
@hospitals = Hospital.all
@stringHospitals = json_hospitals(@hospitals)
Here we can control what objects we are sending to the method json_hospitals. So if you only want to send the ones that are linked, let's say, to a particular location, you just have to change the query. Finally I store the returned json string into my varible @stringHospitals.
<%= editable_field hospitalname, :name, :type => 'select', :submit => 'OK', :data => @stringHospitals %>
Note that in the :data field I'm using the @stringHospitals I just created.
That's it! I hope this helps!!!! :)
Again thank you so much for your gem it has helped a lot! :)
Big thanks, acemacu.
The select now works, only problem left is that i use the select to change an associated user (model) on the base model. When i use it like this :
<%= editable_field @property, :associate, :type => 'select', :submit => 'OK', :data => @json_users %>
...then it only displays a '#', even though you can click on it to get to the select. The controller complains about expecting a user and getting a string, so i change it to this:
<%= editable_field @property, :associate_id, :type => 'select', :submit => 'OK', :data => @json_users %>
This actually works and changes the field in the database. But then only the ID of the user is shown, not his name. Any idea how to get around this?
//edit
Also, how can I tell it to select a certain entry in the select? just adding ' :select => @property.associate.id ' doesnt work :/
could you provide an example of how to hook up select list data? for example, I have:
<%= f.select :state, options_for_select( [[ "Select a State", "" ]] + State::NAMES )%>
in my form view for selecting states. How would I replicate this with the gem? The jeditable documentation shows how to load a url of that data, which I suppose I could do, but, I'm assuming it is possible to pull in something similar to what I have above.
Thanks in advance.