when to render with instance variable, and when to redirect?
My new and edit pages depend on an @important_data instance variable that
is not used in the create and update actions.
As a result my page can't render the new page upon failure.
def create
@my_object = MyObject.new(params[:my_object])
if @my_object.save
redirect_to root_path
else
render action: "new"
#this can't render because the page asks for an @important_data
variable that's not defined.
end
end
Which of the two solutions below should I choose? What are the
advantages/disadvantages of each?
OPTION 1: declare @important_data prior to render
def create
@my_object = MyObject.new(params[:my_object])
if @my_object.save
redirect_to root_path
else
@important_data = ImportantData.all
render action: "new"
end
end
OPTION 2: Redirect
def create
@my_object = MyObject.new(params[:my_object])
if @my_object.save
redirect_to root_path
else
redirect_to new_my_object_path
end
end
No comments:
Post a Comment