Hi everyone,
I’m trying to use ajax to update the selected value of a select element.
Here’s what I have so far:
function RailsModel(model_name)
{
this.model_name = model_name;
}
RailsModel.prototype.getAttributeById = function(attribute, id)
{
Ajax.Request(’/models/get_attribute_by_id/’ + this.model_name + ‘/’ +
attribute + ‘/’ + id,
{
onSuccess:function(response){
this.currentValue = response.responseText;
}
}
);
}
var model = new RailsModel(‘user’);
$(‘some_element’).railsModel = model;
$(‘some_element’).getAttributeById(‘position’, 4);
I’m not entirely sure that this works. However, my question is, how to
I
actually load the correct model in Rails when this request is received?
The URL will be /models/get_attribute_by_id/user/position/4 - I can set
up
the routing so that the params “model”, “attribute”, and “id” will be
set.
But how do I take the value of params[:model], which will be a string,
and
use it to load the right model AND to use that model/class?
So, using a string with value of ‘user’, how can I do the equivalent of
require ‘User’ #(this part isn’t that hard)
#accessing User here is the hard part
model_instance = User.find_by_id(params[:id].to_i)
render :text => model_instance.send(params[:attribute]) #Haven’t tried
this
Thanks!
Daniel