to_json

Hi,

I'm having problems using the jsonifier plugin (I'm not using edge) but I'm having trouble using :only, and :except. If I do I get an ArgumentError wrong number of arguments (1 for 0). Here is my code:

  def tutorialData     @tutorialData = Tutorial.find(:all)     render :text => @tutorialData.to_json(:only => :created_at), :layout => false    end

Any suggestions would be much appreciated.

Thanks,

Tim

Hi fernando,

i don't know how jsoinifer plugin works, but after installing this, if you are expecting it to override the default to_json method(provided by rails), then the code which you posted here is perfect, but if that plugin is not overriding the to_json method of rails then the code which you posted here is wrong, as you know (default)to_json method won't accept any kind of argument.

this is what i know, sorry if it's not answering your question.

Good luck.,

Cheers for that. The plugin works in some respects, removing the 'attributes' from every json object. So iI assume that means that rails has found it and it is working but I still can't pass any arguemtns??? One post here:

report that: This gets me past the wrong number of arguments error: format.json { render(:json => @customers.collect { |customer| customer.to_json(:only => :name) } ) }

But I'm not exactly sure whats going on in that code.

once agian, any help would be much appreciated.

Sounds like you just want to customise your json output. So why not do the following?

def to_json   {     :column_name => column_name,     :some_other_name => another_column_name   }.to_json end

This gives you the advantage of a layer of abstraction between your JS code and Rails code, so if your column names change, you needn't change your JavaScript.

Yes customising my JSON output is exactly what I want to do. I only want to pass certain columns (I have a very simple example to output category names) . I tried your suggestion by putting:     def to_json   {     :name => 'name'   }.to_json end Into my model and :

  def tutorialData     @tutorialData = Category.find(:all)     render :text => @tutorialData.to_json( :name), :layout => false   end

into my controller but I still get a "wrong number of arguments (1 for 0)". Sorry if I am missing something quite fundamental but it looks like rails still expects 0 arguments for the .to_json method??

many thanks for your help so far

I defined the to_json method with 0 arguments, so would expect to get that error. The 'name' comes straight from the object. So:

def to_json   {     :name => name # note: no quotation marks   }.to_json end

and

def tutorialData     @tutorialData = Category.find(:all)     render :json => @tutorialData.to_json end

Should do it.