Topic: why do I have this error

def add_category
      @category = Category.new(params[:category])

      if request.post? and @category.save
        flash[:notice] = 'Category successfully created'
      end
     
       redirect_to :action => "settings"
  end
end


and the error is this.....

<%= error_messages_for "category" %>

I am using rails. 2.0.2

Last edited by foottuns (2008-01-29 08:36:26)

Social Network for Developers-Web Dot Dev!
http://www.webdotdev.com

Re: why do I have this error

no one can help me?

Social Network for Developers-Web Dot Dev!
http://www.webdotdev.com

Re: why do I have this error

What error message? I don't see any error message in what you posted.

Re: why do I have this error

as u can see that is the error,

   1. <%= error_messages_for "category" %>

Social Network for Developers-Web Dot Dev!
http://www.webdotdev.com

Re: why do I have this error

def add_category
  @category = Category.new(params[:category])

  if request.post? and @category.save
    flash[:notice] = 'Category successfully created'
  end
     
  redirect_to :action => "settings"
end
end


You have two ends in your def? Maybe that is it?
The error_messages_for is not an error it is code that is located in your view.
Is it not working as expected?

Re: why do I have this error

There are 2 end simply because the second is the controller end.

It doesn't work at all, for instance I validate the presence of the "name" field and when I submit the form without name, the values are nor saved but no message is showed.


Thanks

Social Network for Developers-Web Dot Dev!
http://www.webdotdev.com

Re: why do I have this error

So your questions is not - Why do I have this error? 
It is - why do my errors not show up when using <%= error_messages_for ... %>?

In your controller, you need to tell the action to rerender the page, but your code, regardless of state always does a redirect, which means it reinitiates your object, losing your errors.

To solve it you need something like this:

def add_category
  @category = Category.new(params[:category])

  if request.post? and @category.save  # here I have succeeded and can move on
    flash[:notice] = 'Category successfully created'
    redirect_to :action => "settings"
  else  # here I rerender the page to show the errors captured in the @category object
    flash[:error] = 'Category could not be created'
    render :action => 'add_category'
  end   
end

Re: why do I have this error

yeah, kind of...but thanks a lot any way...

Social Network for Developers-Web Dot Dev!
http://www.webdotdev.com