Reputation: 2261
In my application I have several text_field_tag's, where user can type in, for example, his age, address, name, weight and so on...
After clicking on "update" I verify if the user typed his weight correctly like 45.6 or 112.9 and not like 4ddg.4 or just chars "aaa", for instance.
After verifying and finding that the user typed the weight incorrectly, I want to return back showing that he made an error (I know how to do this) AND to have the field filled with his answers, like he will not have to type his age, address and name one more time. It is like when you are filling some formula on a web page, you click "forward" and then you understand, that you have typed sth wrong, so you "go back one page" and you see the data you typed in.
I hope I explained my idea correctly.
Thanks in advance.
Katja
edit: Or I do not want to redirect even, I want just to show an error written on the page of the formula. (So I need to redirect partial, do not I?)
Upvotes: 0
Views: 107
Reputation: 494
It sounds like you want to use a flash message. Also check out flash.now.
As for keeping the form fields populated, we'd need to see how your controller / view is set up. Also may depend on if you are doing the validation on the model or some other way (javascript?).
Upvotes: 0
Reputation: 1067
I think it'll be the best to show on real example:
def edit
@user = User.find_by_id(params[:id])
end
def update
@user = User.find_by_id(params[:user][:id])
if @user.update_attributes(good_params)
redirect_to edit_user_path(@user), success: 'Параметры обновлены'
else
render 'edit'
end
@user.update_attributes()
throws errors and filled info directly into @user
, thus when you rerender 'edit', you'll have all your information filled, and you can access errors for displaying it.
Upvotes: 1