Here is code in customers controller:
def edit
@customer = Customer.find(params[:id])
@return_to = params[:return_to]
end
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(params[:customer], :as => :roles_new_update)
redirect_to @return_to, :notice => 'Customer was updated successfaully!'
else
render 'edit', :notice => 'Customer was not updated!'
end
end
It is verified that there is value in @return_to in method edit. However there is an error saying: Cannot redirect to nil! for
redirect_to @return_to, :notice => 'Customer was updated successfaully!'
in method update.
Any thoughts about the error? Thanks.
It’s because when you are on the edit action, that is one request to show the form. Then when you submit the form, that is a second request calling the update action. Any states set in an action are confined to that action alone. There’s a few ways to use data across multiple actions:
Because it’s a redirect/return to value, i would suggestion using a session variable such as:
In your edit action, then just refer to that value in your update: