Forum: Ruby on Rails Validation error messages help please

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James W. (Guest)
on 2006-04-05 20:13
I'm having problems with validation error messages appearing in my app.

In Controller:
def permissions
    @roles = Role.find(:all, :order => 'name')
  end

  def new_role

    @role = Role.create(params[:role])

    if request.post? and @role.save

      flash['notice'] = 'Successfully created a new Role'
      redirect_to :action => "permissions"
    else
      flash['notice'] = 'The Role was not created. There was an error
try again'
      render :action => 'permissions'
    end
  end

Basically I have a permissions page that has a form on it to create new
roles. If the role is created correctly it redirects to the permissions
page and displays the flash. When it fails the display is fine but the
URL now shows "accounts/new_role". But I am clearly rendering
permissions not new_role. If I change it to "redirect_to" then it
display the "accounts/permissions" page fine but my validation errors do
not display? Anyone shed some light onto what is the correct way please?
Adam B. (Guest)
on 2006-04-05 21:00
> Basically I have a permissions page that has a form on it to create new
> roles. If the role is created correctly it redirects to the permissions
> page and displays the flash. When it fails the display is fine but the
> URL now shows "accounts/new_role". But I am clearly rendering
> permissions not new_role. If I change it to "redirect_to" then it
> display the "accounts/permissions" page fine but my validation errors do
> not display? Anyone shed some light onto what is the correct way please?

That is how it's supposed to work. :P

The thing is, when you click the submit button you are redirected to the
"new_role" action; when validation fails you're still in the new_role
action. Then render :action doesn't change the page: it just renders the
selected action within your current page, and that's why it displays
your validation errors properly.

I don't think this can cause any problems, nor do I know of any ways
around it.

-Adam
This topic is locked and can not be replied to.