Trouble maintaining session data through render :action

I have a fairly basic form ‘new’ that assigns an @client instance
variable based on information in the session’s user data.

def new
@client = User.find(:first, :conditions => [“id = ?”,
session[:user_id]]).client
end

‘new’ passes on a ‘case’ parameter to ‘create’ in order to create a case
object.

def create
@case = Case.new(params[:case])
if @case.save
redirect_to :action => ‘show’, :id => @case
else
render :action => ‘new’
end
end

If @case is valid, it saves and is effectively redirected. If @case
doesn’t pass validation, however, @client comes up nil when it
re-renders ‘new’, giving me an error loading the view. I need to use
render rather than redirect_to in order to get flash messages (right?).
How might a be able to get around this?

Thanks,

Peter

Set the flash message before the redirect_to and it will be on the page
that
you redirect_to

Ryan wrote:

Set the flash message before the redirect_to and it will be on the page
that
you redirect_to

Okay.

else
  flash[:notice] = 'Something went wrong'
  redirect_to :action => 'new'
end

This doesn’t seem to do anything to <%= error_messages_for ‘case’ %> in
new.rhtml. Is this what you had in mind? Thanks for your response.

Peter

Phillip K. wrote:

flash[:notice] is an entirely different beast than
error_messages_for. error_messages_for are used by models. They are
the things that get created when your validations fail. flash[] on
the other hand, is something that you can use in your controllers to
provide messages back to your users in the view. Many people
actually use :info, :warning, :error in flash[] and have them styled
differently in CSS. To see if your flash message is working
correctly, do something like

<% if flash[:notice] && flash[:notice].strip != ‘’ %>

<%= flash[:notice] %>
<% end %>

Peace,
Phillip

Ah, that’s right. Thanks for clearing that up. I really haven’t worked
with flash messages much. Do you know of way to get error messages using
redirect_to?

flash[:notice] is an entirely different beast than
error_messages_for. error_messages_for are used by models. They are
the things that get created when your validations fail. flash[] on
the other hand, is something that you can use in your controllers to
provide messages back to your users in the view. Many people
actually use :info, :warning, :error in flash[] and have them styled
differently in CSS. To see if your flash message is working
correctly, do something like

<% if flash[:notice] && flash[:notice].strip != ‘’ %>

<%= flash[:notice] %>
<% end %>

Peace,
Phillip

It seems that you want to render the new action and show a flash notice
and
an error_messages_for at the same time, I think this is possible.

@record = Record.new
if @record.save
flash[:notice] = “Success was here '07”
redirect_to record_path
else
flash[:notice] = “Failure was here '07”
render :action => “new”
end

I do believe this will allow you to access error_messages_for :record
AND
the flash[:notice] at the same time.

Ryan wrote:

I do believe this will allow you to access error_messages_for :record
AND
the flash[:notice] at the same time.

I believe this is possible normally. However, render :action doesn’t
seem to work here given the way I have @client defined in terms of
session data.

In your create action, assign @client to a Client object?

On Dec 5, 2007 11:31 AM, Peter M. [email protected]
wrote:

Posted via http://www.ruby-forum.com/.


Ryan B.

In your original create action there is no assign of the @client
variable,
or a user variable. Just a @case. Could you please paste an updated
version?

On Dec 5, 2007 11:48 AM, Peter M. [email protected]
wrote:

go through.

Posted via http://www.ruby-forum.com/.


Ryan B.

Ryan wrote:

In your original create action there is no assign of the @client
variable,

I didn’t read your last post correctly. I’ve added the @client variable
to the create action and everything works great now. I didn’t realize I
had to copy that over from the ‘new’ action. Thanks the help Ryan!

Peter

On 5 Dec 2007, at 02:01, Peter M. wrote:

Ryan wrote:

In your original create action there is no assign of the @client
variable,

I didn’t read your last post correctly. I’ve added the @client
variable
to the create action and everything works great now. I didn’t
realize I
had to copy that over from the ‘new’ action. Thanks the help Ryan!

That was your actual misunderstanding: render :action => ‘foo’ doesn’t
run the foo action. It just takes the template associated with the foo
action and renders it.

Fred

Ryan wrote:

In your create action, assign @client to a Client object?

It already should be. I tried re-factoring it to this:

user = User.find(:first, :conditions => [“id = ?”, session[:user_id]])
@client = Client.find(:first, :conditions => [“id = ?”, user.client ])

but got the same result. The session data just doesn’t seem to want to
go through.

A follow up question on this matter:

My form has a number of fields that are selectively shown or hidden when
a box is checked, etc. When validation fails and the page re-renders,
all of the data is there, but all the form elements that were previously
shown or hidden are back to their initial state.

Is there an option I can throw into render :action => ‘new’ to
conditionally show these page elements based on the @case object? Any
other ways to get around this issue?

Thanks Again,

Peter