I have a selection box and three text input boxes. When the option changes in the selected box, I want to update the three text input boxes with the values related to the selected option. Values are a model object stored on the server.
I am using Rail 3 and trying to keep things in vain, but I can not find a clear way to do this. I have come with some problems, dirty solutions, but nothing is yet clear what will be a good way to tie all this together? In particular, I'm having trouble with the selected element requesting a model object from the server.
Creates this view form:
= form.select: region_id, Region_options_for_select (@ business_location.region),: Prompt = & gt; '- Area -' = form.text_field: city = form.text_field: state ,: label = & gt; 'Province / State' = form.text_field: Country So whenever "Area" changes, I want to populate the areas of city / state / country. Note that the area is also in its own form.
In jQuery, something like this can work:
$ ("Select"). Live ("change", function (e) {e.preventDefault (); $ .ajax ({url: "/ location /" + $ (this) .val (), // I think this is some more data Type: "Jason", Content Type: "Application / Jason; Charset = UTF-8", Success: Function (Data) {// You are returning a JSON object, then just repeat it and repeat it every Bind the field values with attributes} Error: Function (Excerpt, Exception, Status) {// Hold Any Error Here}})}}; towards the guardrail, in the Location Controller # Show:
@location = Location.find (params [: id]) response_to do | Format | Format.json {Render: Jason = & gt; @ Location.to_json,: Status = & gt; 200} End It is almost how it can work. Choose any of the controller / model names that you have for whatever I have made.
Comments
Post a Comment