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...@gmail.com> wrote:
> No ideas?
>
> On Mar 22, 7:42 pm, FabioKreusch<fabi...@gmail.com> 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...@gmail.com> wrote:
>
> > > On Sun, Mar 21, 2010 at 6:27 PM, FabioKreusch<fabi...@gmail.com> 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...@gmail.com> wrote:
> > > > > On Sun, Mar 21, 2010 at 4:41 PM, FabioKreusch<fabi...@gmail.com>
> > > > 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...@gmail.com> 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])
> > > > > > > > @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@googlegroups.com
> > > > > > .
> > > > > > > > To unsubscribe from this group, send email to
> > > > > > > > rubyonrails-talk+unsubscribe@googlegroups.com<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@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@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@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment