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?
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 != ‘’ %>
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.
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?
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!
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.