Reputation: 3459
I'm experiencing a problem when using erb to set the value of an HTML form field. The scenario is that the user has entered an invalid password—I don't want them to have to re-enter their name/email-address/etc., so these should remain in the appropriate form fields.
I'm using erb to do this, as below:
#app.rb
post '/new-user' do
@user = User.new({params})
if @user.save # <-- this will fail if user info is invalid
session[:user_id] = @user.id
redirect '/chitter-newsfeed'
else
flash.now[:notice] = 'Password and confirmation password do not match'
erb :sign_up
end
end
#sign_up.erb
<form action="/new-user" method="post" >
Name:
<input type="text" name="real_name" value=<%[email protected]_name%> ><br>
User Name:
<input type="text" name="user_name" value=<%[email protected]_name%> ><br>
etc.
My problem is that the code only populates the HTML fields up to the first space in whatever string erb gives it. e.g. If @user.real_name
is "Foo Bar Baz"
, only "Foo"
will appear in the "real_name"
input field.
Having tested this, @user.real_name
is not being corrupted—it remains set to "Foo Bar Baz" even after the form has rendered. And if I hard-code the form field's value, it can render a name with spaces. It is something to do with the interplay between erb and the HTML that is breaking this. Any idea what the problem could be?
Upvotes: 0
Views: 961
Reputation: 107107
Just add quotes around the attributes:
Name:
<input type="text" name="real_name" value="<%= @user.real_name %>"><br>
User Name:
<input type="text" name="user_name" value="<%= @user.user_name %>"><br>
Upvotes: 3