def new
@user = User.find(session[:this_user])
@message = Message.new
@people= People.order("is_active DESC, first_name, last_name")
end
When the create action fails validation, I need to set up the above variables again, creating an action that looks like:
def create
@message = Message.new(params[:message])
if @message.save
redirect_to(messages_path, :notice => 'Message was successfully created.')
else
@user = User.find(session[:this_user])
@message = Message.new
@people= People.order("is_active DESC, first_name, last_name")
render :action => "new"
end
end
What is the proper way to remove redundant code (and DRY it)?
Also, the above approach causes the form to forget any input if the validation fails. Is there a better way to do it so that it remembers any information entered?
Rails 3.07, Ruby 1.9.2
Put
@user = User.find(session[:this_user])in abefore_actionlike so:And
@people= People.order("is_active DESC, first_name, last_name")could be ascopelike so: