Daniel Higginbotham
2006-Jun-18 17:57 UTC
[Rails] using javascript to get an attribute using an ID
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
Ezra Zygmuntowicz
2006-Jun-18 19:56 UTC
[Rails] using javascript to get an attribute using an ID
On Jun 18, 2006, at 10:57 AM, Daniel Higginbotham wrote:> > 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! > DanielDaniel- How about something like this: I don''t think you will need to require User at all: render :text => params[:model].constantize.find(params[:id]).send (params[:attribute]) Cheers- -Ezra
Daniel Higginbotham
2006-Jun-18 20:57 UTC
[Rails] using javascript to get an attribute using an ID
That did it - thanks so much! Daniel -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Ezra Zygmuntowicz Sent: Sunday, June 18, 2006 9:56 AM To: rails@lists.rubyonrails.org Subject: Re: [Rails] using javascript to get an attribute using an ID On Jun 18, 2006, at 10:57 AM, Daniel Higginbotham wrote:> > 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! > DanielDaniel- How about something like this: I don''t think you will need to require User at all: render :text => params[:model].constantize.find(params[:id]).send (params[:attribute]) Cheers- -Ezra _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails