Hi all! I''m playing with Rails 3, and on one of my controllers I have the following update method defined: def update @category = @user.categories.find(params[:id]) @category.update_attributes(params[:ecategory]) @category.save respond_with @category end I''m using Jquery to interact with the controllers, and when I do an ajax PUT, the update action is correctly executed (the fields are updated), but the action returns an empty JSON object, which causes the Jquery ajax function to fall into an error. I''m I doing something wrong? Thank you all, Fabio. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Mar 21, 2010 at 9:44 AM, Fabio Kreusch <fabiokr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all! > > I''m playing with Rails 3, and on one of my controllers I have the > following update method defined: > > def update > @category = @user.categories.find(params[:id]) > @category.update_attributes(params[:ecategory]) > @category.save > respond_with @category > end > >Fabio, you should not be calling save within the update action because ''update_attributes'' calls save implicitly. Thus, you should do something like the following: class CategoriesController < ApplicationController::Base respond_to :html, :json def update @category = @user.categories.find( params[:id] ) if @category.update_attributes( params[:category] ) flash[:notice] = ''Category was successfully created.'' end respond_with( @category ) end end Good luck, -Conrad I''m using Jquery to interact with the controllers, and when I do an> ajax PUT, the update action is correctly executed (the fields are > updated), but the action returns an empty JSON object, which causes > the Jquery ajax function to fall into an error. > > I''m I doing something wrong? > > Thank you all, > Fabio. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Conrad, I have removed the save call from the code, but it continues to return an empty JSON on success case. Any more clues? On Mar 21, 3:37 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Mar 21, 2010 at 9:44 AM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi all! > > > I''m playing with Rails 3, and on one of my controllers I have the > > following update method defined: > > > def update > > @category = @user.categories.find(params[:id]) > > -vwTnWBwdg3FcQ7ALCyhmPw@public.gmane.org_attributes(params[:ecategory]) > > -vwTnWBwdg3GzOeKtxYPyNA@public.gmane.org > > respond_with @category > > end > > Fabio, you should not be calling save within the update > action because ''update_attributes'' calls save implicitly. > Thus, you should do something like the following: > > class CategoriesController < ApplicationController::Base > > respond_to :html, :json > > def update > > @category = @user.categories.find( params[:id] ) > > if @category.update_attributes( params[:category] ) > > flash[:notice] = ''Category was successfully created.'' > > end > > respond_with( @category ) > > end > > end > > Good luck, > > -Conrad > > I''m using Jquery to interact with the controllers, and when I do an > > > > > ajax PUT, the update action is correctly executed (the fields are > > updated), but the action returns an empty JSON object, which causes > > the Jquery ajax function to fall into an error. > > > I''m I doing something wrong? > > > Thank you all, > > Fabio. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Mar 21, 2010 at 4:41 PM, Fabio Kreusch <fabiokr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Conrad, I have removed the save call from the code, but it > continues to return an empty JSON on success case. > Any more clues? > >Fabio, what happens in the Rails 3 console? For example, you can do the following in the Rails console: a) get a user b) retrieve a category from the user''s list of categories c) convert the result in (b) into json Next, what does the jQuery code look like? -Conrad> On Mar 21, 3:37 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On Sun, Mar 21, 2010 at 9:44 AM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > Hi all! > > > > > I''m playing with Rails 3, and on one of my controllers I have the > > > following update method defined: > > > > > def update > > > @category = @user.categories.find(params[:id]) > > > @category.update_attributes(params[:ecategory]) > > > @category.save > > > respond_with @category > > > end > > > > Fabio, you should not be calling save within the update > > action because ''update_attributes'' calls save implicitly. > > Thus, you should do something like the following: > > > > class CategoriesController < ApplicationController::Base > > > > respond_to :html, :json > > > > def update > > > > @category = @user.categories.find( params[:id] ) > > > > if @category.update_attributes( params[:category] ) > > > > flash[:notice] = ''Category was successfully created.'' > > > > end > > > > respond_with( @category ) > > > > end > > > > end > > > > Good luck, > > > > -Conrad > > > > I''m using Jquery to interact with the controllers, and when I do an > > > > > > > > > ajax PUT, the update action is correctly executed (the fields are > > > updated), but the action returns an empty JSON object, which causes > > > the Jquery ajax function to fall into an error. > > > > > I''m I doing something wrong? > > > > > Thank you all, > > > Fabio. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Conrad, I have tried Rails console, and while trying to convert to json the results were the expected. This is the Jquery code used on my ajax form: $(''#category_form'').submit(function() { var form = $(this); $.ajax({ url: form.attr(''action''), type: ''POST'', data: form.serialize(), success: function(data, status, req) { reloadCategories(); }, error: function(req, status, error) { setFormErrors(form, req); } }); return false; }); This function is used with both create and update forms, but on create everything goes ok and it falls on the success function, while during update, the JSON result comes blank, Jquery gets an error while trying to parse the empty Json object and it falls on the error function. .. On Mar 21, 10:16 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Mar 21, 2010 at 4:41 PM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Thanks Conrad, I have removed the save call from the code, but it > > continues to return an empty JSON on success case. > > Any more clues? > > Fabio, what happens in the Rails 3 console? For example, you can do the > following in the Rails console: > > a) get a user > b) retrieve a category from the user''s list of categories > c) convert the result in (b) into json > > Next, what does the jQuery code look like? > > -Conrad > > > > > On Mar 21, 3:37 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Sun, Mar 21, 2010 at 9:44 AM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > Hi all! > > > > > I''m playing with Rails 3, and on one of my controllers I have the > > > > following update method defined: > > > > > def update > > > > @category = @user.categories.find(params[:id]) > > > > -vwTnWBwdg3FcQ7ALCyhmPw@public.gmane.org_attributes(params[:ecategory]) > > > > -vwTnWBwdg3GzOeKtxYPyNA@public.gmane.org > > > > respond_with @category > > > > end > > > Fabio, you should not be calling save within the update > > > action because ''update_attributes'' calls save implicitly. > > > Thus, you should do something like the following: > > > > class CategoriesController < ApplicationController::Base > > > > respond_to :html, :json > > > > def update > > > > @category = @user.categories.find( params[:id] ) > > > > if @category.update_attributes( params[:category] ) > > > > flash[:notice] = ''Category was successfully created.'' > > > > end > > > > respond_with( @category ) > > > > end > > > > end > > > > Good luck, > > > > -Conrad > > > > I''m using Jquery to interact with the controllers, and when I do an > > > > > ajax PUT, the update action is correctly executed (the fields are > > > > updated), but the action returns an empty JSON object, which causes > > > > the Jquery ajax function to fall into an error. > > > > > I''m I doing something wrong? > > > > > Thank you all, > > > > Fabio. > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "Ruby on Rails: Talk" group. > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com > > . > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > e@googlegroups.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Mar 21, 2010 at 6:27 PM, Fabio Kreusch <fabiokr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Conrad, > I have tried Rails console, and while trying to convert to json the > results were the expected. > > This is the Jquery code used on my ajax form: > > $(''#category_form'').submit(function() { > var form = $(this); > $.ajax({ > url: form.attr(''action''), > type: ''POST'', > data: form.serialize(), > success: function(data, status, req) { > reloadCategories(); > }, > error: function(req, status, error) { > setFormErrors(form, req); > } > }); > return false; > }); > > This function is used with both create and update forms, but on create > everything goes ok and it falls on the success function, while during > update, the JSON result comes blank, Jquery gets an error while trying > to parse the empty Json object and it falls on the error function. > .. >Can you post the view template of the form? -Conrad> > On Mar 21, 10:16 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On Sun, Mar 21, 2010 at 4:41 PM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > Thanks Conrad, I have removed the save call from the code, but it > > > continues to return an empty JSON on success case. > > > Any more clues? > > > > Fabio, what happens in the Rails 3 console? For example, you can do the > > following in the Rails console: > > > > a) get a user > > b) retrieve a category from the user''s list of categories > > c) convert the result in (b) into json > > > > Next, what does the jQuery code look like? > > > > -Conrad > > > > > > > > > On Mar 21, 3:37 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Sun, Mar 21, 2010 at 9:44 AM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > > Hi all! > > > > > > > I''m playing with Rails 3, and on one of my controllers I have the > > > > > following update method defined: > > > > > > > def update > > > > > @category = @user.categories.find(params[:id]) > > > > > @category.update_attributes(params[:ecategory]) > > > > > @category.save > > > > > respond_with @category > > > > > end > > > > > Fabio, you should not be calling save within the update > > > > action because ''update_attributes'' calls save implicitly. > > > > Thus, you should do something like the following: > > > > > > class CategoriesController < ApplicationController::Base > > > > > > respond_to :html, :json > > > > > > def update > > > > > > @category = @user.categories.find( params[:id] ) > > > > > > if @category.update_attributes( params[:category] ) > > > > > > flash[:notice] = ''Category was successfully created.'' > > > > > > end > > > > > > respond_with( @category ) > > > > > > end > > > > > > end > > > > > > Good luck, > > > > > > -Conrad > > > > > > I''m using Jquery to interact with the controllers, and when I do an > > > > > > > ajax PUT, the update action is correctly executed (the fields are > > > > > updated), but the action returns an empty JSON object, which causes > > > > > the Jquery ajax function to fall into an error. > > > > > > > I''m I doing something wrong? > > > > > > > Thank you all, > > > > > Fabio. > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "Ruby on Rails: Talk" group. > > > > > To post to this group, send email to > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > . > > > > > To unsubscribe from this group, send email to > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > e@googlegroups.com> > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This is it (HAML): #category-form - form_for @category, :remote => true do |f| %fieldset %legend= t(''activerecord.models.category'') %p = f.label :name %br = f.text_field :name %p %button{:type => ''submit'', :class => ''positive''}t(''dashboard.save'') The same form is used both for create and update. I use this Jquery code to change between create and update actions (this is called on click of the ''new category'' button'', or on the on click of an existing category: attr = $(this).data(''attributes''); form = categoryForm.find(''form''); form.find(''input[name=_method]'').remove(); if(attr.id) { action = ''/categories/''+attr.id; form.append($(''<input/>'', {type: ''hidden'', name: ''_method'', value: ''put''})); } else { action = ''/expense_categories''; } $.each(attr, function(i, val){ categoryForm.find(''[id$=''+i+'']'').val(val); }); form.attr(''action'', action); On Mar 22, 2:05 am, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Mar 21, 2010 at 6:27 PM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi Conrad, > > I have tried Rails console, and while trying to convert to json the > > results were the expected. > > > This is the Jquery code used on my ajax form: > > > $(''#category_form'').submit(function() { > > var form = $(this); > > $.ajax({ > > url: form.attr(''action''), > > type: ''POST'', > > data: form.serialize(), > > success: function(data, status, req) { > > reloadCategories(); > > }, > > error: function(req, status, error) { > > setFormErrors(form, req); > > } > > }); > > return false; > > }); > > > This function is used with both create and update forms, but on create > > everything goes ok and it falls on the success function, while during > > update, the JSON result comes blank, Jquery gets an error while trying > > to parse the empty Json object and it falls on the error function. > > .. > > Can you post the view template of the form? > > -Conrad > > > > > > > On Mar 21, 10:16 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Sun, Mar 21, 2010 at 4:41 PM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > Thanks Conrad, I have removed the save call from the code, but it > > > > continues to return an empty JSON on success case. > > > > Any more clues? > > > > Fabio, what happens in the Rails 3 console? For example, you can do the > > > following in the Rails console: > > > > a) get a user > > > b) retrieve a category from the user''s list of categories > > > c) convert the result in (b) into json > > > > Next, what does the jQuery code look like? > > > > -Conrad > > > > > On Mar 21, 3:37 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Sun, Mar 21, 2010 at 9:44 AM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > wrote: > > > > > > Hi all! > > > > > > > I''m playing with Rails 3, and on one of my controllers I have the > > > > > > following update method defined: > > > > > > > def update > > > > > > @category = @user.categories.find(params[:id]) > > > > > > -vwTnWBwdg3FcQ7ALCyhmPw@public.gmane.org_attributes(params[:ecategory]) > > > > > > -vwTnWBwdg3GzOeKtxYPyNA@public.gmane.org > > > > > > respond_with @category > > > > > > end > > > > > Fabio, you should not be calling save within the update > > > > > action because ''update_attributes'' calls save implicitly. > > > > > Thus, you should do something like the following: > > > > > > class CategoriesController < ApplicationController::Base > > > > > > respond_to :html, :json > > > > > > def update > > > > > > @category = @user.categories.find( params[:id] ) > > > > > > if @category.update_attributes( params[:category] ) > > > > > > flash[:notice] = ''Category was successfully created.'' > > > > > > end > > > > > > respond_with( @category ) > > > > > > end > > > > > > end > > > > > > Good luck, > > > > > > -Conrad > > > > > > I''m using Jquery to interact with the controllers, and when I do an > > > > > > > ajax PUT, the update action is correctly executed (the fields are > > > > > > updated), but the action returns an empty JSON object, which causes > > > > > > the Jquery ajax function to fall into an error. > > > > > > > I''m I doing something wrong? > > > > > > > Thank you all, > > > > > > Fabio. > > > > > > > -- > > > > > > You received this message because you are subscribed to the Google > > > > Groups > > > > > > "Ruby on Rails: Talk" group. > > > > > > To post to this group, send email to > > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > > . > > > > > > To unsubscribe from this group, send email to > > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > > e@googlegroups.com> > > > > > > . > > > > > > For more options, visit this group at > > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "Ruby on Rails: Talk" group. > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com > > . > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > e@googlegroups.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No ideas? On Mar 22, 7:42 pm, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is it (HAML): > > #category-form > - form_for @category, :remote => true do |f| > %fieldset > %legend= t(''activerecord.models.category'') > %p > = f.label :name > %br > = f.text_field :name > %p > %button{:type => ''submit'', :class => ''positive''}> t(''dashboard.save'') > > The same form is used both for create and update. I use this Jquery > code to change between create and update actions (this is called on > click of the ''new category'' button'', or on the on click of an existing > category: > > attr = $(this).data(''attributes''); > form = categoryForm.find(''form''); > form.find(''input[name=_method]'').remove(); > > if(attr.id) { > action = ''/categories/''+attr.id; > form.append($(''<input/>'', {type: ''hidden'', name: ''_method'', value: > ''put''})); > } else { > action = ''/expense_categories''; > } > > $.each(attr, function(i, val){ > categoryForm.find(''[id$=''+i+'']'').val(val); > }); > > form.attr(''action'', action); > > On Mar 22, 2:05 am, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Sun, Mar 21, 2010 at 6:27 PM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi Conrad, > > > I have tried Rails console, and while trying to convert to json the > > > results were the expected. > > > > This is the Jquery code used on my ajax form: > > > > $(''#category_form'').submit(function() { > > > var form = $(this); > > > $.ajax({ > > > url: form.attr(''action''), > > > type: ''POST'', > > > data: form.serialize(), > > > success: function(data, status, req) { > > > reloadCategories(); > > > }, > > > error: function(req, status, error) { > > > setFormErrors(form, req); > > > } > > > }); > > > return false; > > > }); > > > > This function is used with both create and update forms, but on create > > > everything goes ok and it falls on the success function, while during > > > update, the JSON result comes blank, Jquery gets an error while trying > > > to parse the empty Json object and it falls on the error function. > > > .. > > > Can you post the view template of the form? > > > -Conrad > > > > On Mar 21, 10:16 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Sun, Mar 21, 2010 at 4:41 PM, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > > Thanks Conrad, I have removed the save call from the code, but it > > > > > continues to return an empty JSON on success case. > > > > > Any more clues? > > > > > Fabio, what happens in the Rails 3 console? For example, you can do the > > > > following in the Rails console: > > > > > a) get a user > > > > b) retrieve a category from the user''s list of categories > > > > c) convert the result in (b) into json > > > > > Next, what does the jQuery code look like? > > > > > -Conrad > > > > > > On Mar 21, 3:37 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > On Sun, Mar 21, 2010 at 9:44 AM, Fabio Kreusch <fabi...@gmail.com> > > > > > wrote: > > > > > > > Hi all! > > > > > > > > I''m playing with Rails 3, and on one of my controllers I have the > > > > > > > following update method defined: > > > > > > > > def update > > > > > > > @category = @user.categories.find(params[:id]) > > > > > > > -vwTnWBwdg3FcQ7ALCyhmPw@public.gmane.org_attributes(params[:ecategory]) > > > > > > > -vwTnWBwdg3GzOeKtxYPyNA@public.gmane.org > > > > > > > respond_with @category > > > > > > > end > > > > > > Fabio, you should not be calling save within the update > > > > > > action because ''update_attributes'' calls save implicitly. > > > > > > Thus, you should do something like the following: > > > > > > > class CategoriesController < ApplicationController::Base > > > > > > > respond_to :html, :json > > > > > > > def update > > > > > > > @category = @user.categories.find( params[:id] ) > > > > > > > if @category.update_attributes( params[:category] ) > > > > > > > flash[:notice] = ''Category was successfully created.'' > > > > > > > end > > > > > > > respond_with( @category ) > > > > > > > end > > > > > > > end > > > > > > > Good luck, > > > > > > > -Conrad > > > > > > > I''m using Jquery to interact with the controllers, and when I do an > > > > > > > > ajax PUT, the update action is correctly executed (the fields are > > > > > > > updated), but the action returns an empty JSON object, which causes > > > > > > > the Jquery ajax function to fall into an error. > > > > > > > > I''m I doing something wrong? > > > > > > > > Thank you all, > > > > > > > Fabio. > > > > > > > > -- > > > > > > > You received this message because you are subscribed to the Google > > > > > Groups > > > > > > > "Ruby on Rails: Talk" group. > > > > > > > To post to this group, send email to > > > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > > > . > > > > > > > To unsubscribe from this group, send email to > > > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > > > e@googlegroups.com> > > > > > > > . > > > > > > > For more options, visit this group at > > > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "Ruby on Rails: Talk" group. > > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com > > > . > > > > > To unsubscribe from this group, send email to > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > e@googlegroups.com> > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > > > You received this message because you are subscribed to the Google Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I am still stuck with this. Does anyone knows if this is the expected beavior for Rails? Should respond_with on update really return an empty json object? On Mar 23, 7:17 pm, Fabio Kreusch <fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> No ideas? > > On Mar 22, 7:42 pm, FabioKreusch<fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > This is it (HAML): > > > #category-form > > - form_for @category, :remote => true do |f| > > %fieldset > > %legend= t(''activerecord.models.category'') > > %p > > = f.label :name > > %br > > = f.text_field :name > > %p > > %button{:type => ''submit'', :class => ''positive''}> > t(''dashboard.save'') > > > The same form is used both for create and update. I use this Jquery > > code to change between create and update actions (this is called on > > click of the ''new category'' button'', or on the on click of an existing > > category: > > > attr = $(this).data(''attributes''); > > form = categoryForm.find(''form''); > > form.find(''input[name=_method]'').remove(); > > > if(attr.id) { > > action = ''/categories/''+attr.id; > > form.append($(''<input/>'', {type: ''hidden'', name: ''_method'', value: > > ''put''})); > > } else { > > action = ''/expense_categories''; > > } > > > $.each(attr, function(i, val){ > > categoryForm.find(''[id$=''+i+'']'').val(val); > > }); > > > form.attr(''action'', action); > > > On Mar 22, 2:05 am, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Sun, Mar 21, 2010 at 6:27 PM, FabioKreusch<fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi Conrad, > > > > I have tried Rails console, and while trying to convert to json the > > > > results were the expected. > > > > > This is the Jquery code used on my ajax form: > > > > > $(''#category_form'').submit(function() { > > > > var form = $(this); > > > > $.ajax({ > > > > url: form.attr(''action''), > > > > type: ''POST'', > > > > data: form.serialize(), > > > > success: function(data, status, req) { > > > > reloadCategories(); > > > > }, > > > > error: function(req, status, error) { > > > > setFormErrors(form, req); > > > > } > > > > }); > > > > return false; > > > > }); > > > > > This function is used with both create and update forms, but on create > > > > everything goes ok and it falls on the success function, while during > > > > update, the JSON result comes blank, Jquery gets an error while trying > > > > to parse the empty Json object and it falls on the error function. > > > > .. > > > > Can you post the view template of the form? > > > > -Conrad > > > > > On Mar 21, 10:16 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Sun, Mar 21, 2010 at 4:41 PM, FabioKreusch<fabi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > wrote: > > > > > > Thanks Conrad, I have removed the save call from the code, but it > > > > > > continues to return an empty JSON on success case. > > > > > > Any more clues? > > > > > > Fabio, what happens in the Rails 3 console? For example, you can do the > > > > > following in the Rails console: > > > > > > a) get a user > > > > > b) retrieve a category from the user''s list of categories > > > > > c) convert the result in (b) into json > > > > > > Next, what does the jQuery code look like? > > > > > > -Conrad > > > > > > > On Mar 21, 3:37 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > On Sun, Mar 21, 2010 at 9:44 AM, FabioKreusch<fabi...@gmail.com> > > > > > > wrote: > > > > > > > > Hi all! > > > > > > > > > I''m playing with Rails 3, and on one of my controllers I have the > > > > > > > > following update method defined: > > > > > > > > > def update > > > > > > > > @category = @user.categories.find(params[:id]) > > > > > > > > -vwTnWBwdg3FcQ7ALCyhmPw@public.gmane.org_attributes(params[:ecategory]) > > > > > > > > -vwTnWBwdg3GzOeKtxYPyNA@public.gmane.org > > > > > > > > respond_with @category > > > > > > > > end > > > > > > > Fabio, you should not be calling save within the update > > > > > > > action because ''update_attributes'' calls save implicitly. > > > > > > > Thus, you should do something like the following: > > > > > > > > class CategoriesController < ApplicationController::Base > > > > > > > > respond_to :html, :json > > > > > > > > def update > > > > > > > > @category = @user.categories.find( params[:id] ) > > > > > > > > if @category.update_attributes( params[:category] ) > > > > > > > > flash[:notice] = ''Category was successfully created.'' > > > > > > > > end > > > > > > > > respond_with( @category ) > > > > > > > > end > > > > > > > > end > > > > > > > > Good luck, > > > > > > > > -Conrad > > > > > > > > I''m using Jquery to interact with the controllers, and when I do an > > > > > > > > > ajax PUT, the update action is correctly executed (the fields are > > > > > > > > updated), but the action returns an empty JSON object, which causes > > > > > > > > the Jquery ajax function to fall into an error. > > > > > > > > > I''m I doing something wrong? > > > > > > > > > Thank you all, > > > > > > > > Fabio. > > > > > > > > > -- > > > > > > > > You received this message because you are subscribed to the Google > > > > > > Groups > > > > > > > > "Ruby on Rails: Talk" group. > > > > > > > > To post to this group, send email to > > > > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > > > > . > > > > > > > > To unsubscribe from this group, send email to > > > > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > > > > e@googlegroups.com> > > > > > > > > . > > > > > > > > For more options, visit this group at > > > > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > > > -- > > > > > > You received this message because you are subscribed to the Google > > > > Groups > > > > > > "Ruby on Rails: Talk" group. > > > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com > > > > . > > > > > > To unsubscribe from this group, send email to > > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > > e@googlegroups.com> > > > > > > . > > > > > > For more options, visit this group at > > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > > You received this message because you are subscribed to the Google Groups > > > > "Ruby on Rails: Talk" group. > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com. > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fabio Kreusch wrote:> I am still stuck with this. Does anyone knows if this is the expected > beavior for Rails? Should respond_with on update really return an > empty json object?I pretty sure the Rails behavior is as intended. Rails basically says the save was successful by returning a 200 status code. jQuery however, considers the request successful if it gets a 200 status code AND some valid json content. Personally, I like the Rails way better here. But nonetheless, I''m currently stuck with the same problem. An ugly (but working!) solution would be to replace your respond_with with this render :json => { :ok => true } Did you find a nicer solution? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi all, I too have same problem. Problem: I am getting "successfully stored." alert message, but getting "null" value in suceeding alert message. whether as expected data is {"id" => "xx"} the code is as follows ----------------controller code------------------------ @tmp = {} @tmp[:id] = "xx" respond_with do |format| format.html{ render :json => @tmp } format.js end -----------------javascript---------------------------- $.ajax({ url: image.saveUrl, type: ''POST'', data: form.serialize(), error: function(e) { alert("An error occured saving that note.") }, success: function(data, status, req) { alert(" successfully stored."); alert(data); }, dataType: "json" }); } Can anyone help me. Thank u, Aashish -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I found the solution. use firebug to see response. Use image as attachment for help -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.