Topic: Validate and Ajax

It's a bit complex to add my code as I don't know what to add. So let me ask a general question instead.

I have a div where I render a partial with a form via Ajax

Re: Validate and Ajax

Inside your div you need to have another div to display validation errors (eg. 'error-div')

and replace the div with 'error_messages_for('model_name') - for this you can use rjs

page.replace_html 'error-div', error_messages_for('model_name')

Re: Validate and Ajax

virajt wrote:

Inside your div you need to have another div to display validation errors (eg. 'error-div')

and replace the div with 'error_messages_for('model_name') - for this you can use rjs

page.replace_html 'error-div', error_messages_for('model_name')

That I understand. What I don't know is how to come back to the view with the form, that was loaded with a :partial. This is my create.

 def create
    @group = Group.new(params[:group])
    if @group.save
      flash[:notice] = 'Group was created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

The new thing is that I keep everything in the view index. When adding a new group I load the new as a partial, so that page is now called _new.

 def new
    render :update do |page|
       page.visual_effect :appear, 'showcontent'
       page.replace_html 'showcontent', :partial => 'admingroup/new'
     end
  end

I need the create go back to the index and load the partial 'admingroup/new' but with the error_message in place (as said above). How do I do that?

Re: Validate and Ajax

you say you call everything via AJAX, so the create action too?
Well you don't have to redirect then
just render :update in the create action

def create
  @group = Group.new(params[:group])
  if @group.save
    flash[:notice] = 'Group was created.'
    redirect_to :action => 'list'
  else
    render :update do |page|
      page.replace_html 'showcontent', :partial => 'admingroup/new'
      page.visual_effect :appear, 'showcontent'
      page.replace_html 'error-div', error_messages_for('group')
    end
  end
end

Note: for this to work, you can't have an :update => "somediv" defined in your form_remote options.

Also, as you use AJAX, i guess you won't even have to render the form .... because its this form that the user used submit to the create action, so it's still on his screen isn't it?), you only have to render back the error_messages_for ...

Re: Validate and Ajax

Duplex wrote:

you say you call everything via AJAX, so the create action too?
Well you don't have to redirect then
just render :update in the create action

def create
  @group = Group.new(params[:group])
  if @group.save
    flash[:notice] = 'Group was created.'
    redirect_to :action => 'list'
  else
    render :update do |page|
      page.replace_html 'showcontent', :partial => 'admingroup/new'
      page.visual_effect :appear, 'showcontent'
      page.replace_html 'error-div', error_messages_for('group')
    end
  end
end

Note: for this to work, you can't have an :update => "somediv" defined in your form_remote options.

Also, as you use AJAX, i guess you won't even have to render the form .... because its this form that the user used submit to the create action, so it's still on his screen isn't it?), you only have to render back the error_messages_for ...

I tried this and all I got was plain text, not rendered as html. Maybe it has something to do with your note. I don't quite get that but maybe I have to read it more than once. smile

Re: Validate and Ajax

I tried the form_remote_tag and it's better. Thanks.