Forum: Ruby on Rails How to carry parameters to a new page?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F1a41eb20260a0f7442a00c70595e512?d=identicon&s=25 Dzei (Guest)
on 2006-01-09 14:01

I have a page where user can register him/herself to the site.

If the user enters something wrong, or leaves blank textboxes, I want to
reload the page and tell the user what went wrong. Also the
textbox-values the user already entered, should be kept in the

For example, in my user-controller:

if @username.length == 0
   redirect_to :action => 'registeruser', :message => "You must give an

This gives the url:
"/user/registeruser?message=You+must+give+an+username" and I can show
the message with <%=h @params['message']%>.

Is this THE way to go, just add the values from textboxes to the url
also? Or is there a way I can do this without using the url-parameters?
531eb73f8fbf05a197721d02b4e6aadb?d=identicon&s=25 Bogdan Ionescu (Guest)
on 2006-01-09 15:14
(Received via mailing list)
You could do:

if @username.length == 0
  @session[:error]="You must give an username"
  redirect_to :action => 'registeruser'
and then use @session[:error] to access the error
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2006-01-09 16:09
(Received via mailing list)

On 9.1.2006, at 15.01, Dzei wrote:

> For example, in my user-controller:
> Is this THE way to go, just add the values from textboxes to the url
> also? Or is there a way I can do this without using the url-
> parameters?

The Rails way of doing this is not to redirect. If there's something
wrong with the input data, just re-display the same form:

in the controller:

@user =[:user])

	redirect_to somewhere_else
	# the user input was invalid = "There was a problem with your input"
	render :action => "add_user"

Now the add_user template (the same you used in the original form
page) will have an invalid @user object at hand. If you used the
Rails form helper methods like text_field (you did, didn't you?), the
fields with error will be automatically marked as erroneous. You can
also display error_messages_for, which will output a list of all
fields that were invalid.

Oh, and you shouldn't have to do things like

> if @username.length == 0
>    redirect_to :action => 'registeruser', :message => "You must
> give an
> username"

yourself. Validations like this belong to the model and the above-
mentioned depends on the model validations. See the api docs for
validations [1] for more info.


59b605fe5702090bceb33dc663aea117?d=identicon&s=25 Chris Hoffman (Guest)
on 2006-01-09 16:57
(Received via mailing list)
You should really read Agile Web Development with Rails.  Using flash
is one of the first things discussed, and if your form was built with
scaffolding, then the support is already there.
F1a41eb20260a0f7442a00c70595e512?d=identicon&s=25 Jarno Kiviaho (jarkki)
on 2006-01-09 17:57
Thank you all for your answers.

And yes I ordered the Agile Web Development with Rails last week. I hope
it ships soon. :)
This topic is locked and can not be replied to.