Hi all,
After validation of a form field rails keeps the entered data in the
form. The strange thing is that the entered data are kept in some fields
but not in every field. Although the
-tags are almost the same. In
the following form field the entered data is kept after the validation
| Name: |
<%=text_field("customer", "first_name")%> |
In the following the value is lost after validation
Strasse: |
<%=text_field("contact_detail", "address1")%> |
Did anyone struggle over the same issue?
On Oct 5, 1:07 pm, Yu Co [email protected] wrote:
After validation of a form field rails keeps the entered data in the
form. The strange thing is that the entered data are kept in some fields
but not in every field. Although the -tags are almost the same. In
the following form field the entered data is kept after the validation
Name:
<%=text_field("customer", "first_name")%>
In the following the value is lost after validation
Strasse:
<%=text_field("contact_detail", "address1")%>
Can you post your controller code? It looks like the @contact_detail
object isn’t being populated. It’s probably something to do with the
order that you’re assigning and validating things.
Chris
Chris M. wrote:
On Oct 5, 1:07 pm, Yu Co [email protected] wrote:
After validation of a form field rails keeps the entered data in the
form. The strange thing is that the entered data are kept in some fields
but not in every field. Although the -tags are almost the same. In
the following form field the entered data is kept after the validation
Name:
<%=text_field("customer", "first_name")%>
In the following the value is lost after validation
Strasse:
<%=text_field("contact_detail", "address1")%>
Can you post your controller code? It looks like the @contact_detail
object isn’t being populated. It’s probably something to do with the
order that you’re assigning and validating things.
Chris
Hi Chris,
the relevant code piece of the corresponding controller looks like this:
â??
.
.
.
def create
@contact_detail = ContactDetail.new(params[:contact_detail])
@customer = Customer.new(params[:customer])
@customer.contact_detail = @contact_detail
.
.
.
if @customer.save
.
.
session[‘user’] = @customer
session[:customer] = nil
.
.
else
begin
session[:errors] = @errors
end
end
â?¦
â?
Yu Co wrote:
else
begin
session[:errors] = @errors
end
end
I can’t see how you’re redisplaying the form. Are you doing a
render(:action => :new)? Are you redirecting back to the ‘new’ action
using redirect_to(:action => :new)? Or do you have a separate
create.rhtml template?
Chris
Chris M. wrote:
Yu Co wrote:
else
begin
session[:errors] = @errors
end
end
I can’t see how you’re redisplaying the form. Are you doing a
render(:action => :new)? Are you redirecting back to the ‘new’ action
using redirect_to(:action => :new)? Or do you have a separate
create.rhtml template?
Chris
aaa sorry for that. The relevant code piece looks like that:
session[:errors] = @errors
session[:customer] = @customer
redirect_to :controller => 'auth', :action => 'register'
and register.html:
<%= start_form_tag :controller => ‘customer’, :action => ‘create’ %>
<%= render :partial => ‘customer/registrationform’ %>
| <%= submit_tag "Register and Order"
%> |
<%= end_form_tag %>
and the controller auth looks like this:
def register
@customer = session[:customer] == nil ? Customer.new :
session[:customer]
@errors = session[:errors]
end
I checked the values within ‘register’ action. And indeed the entered
data in the form is available under @customer.contact_detail. But after
submitting the form the contact_detail dates are not shown but the
customer dates.
Chris M. wrote:
Yu Co wrote:
data in the form is available under @customer.contact_detail. But after
submitting the form the contact_detail dates are not shown but the
customer dates.
Your view is expecting to find the contact_detail object in
@contact_detail, but you’re not setting this in the controller code.
Try adding:
@contact_detail = @customer.contact_detail
to your register action.
Chris
Hi Chris,
many thanks to this advise. Thatâ??s the solution. I supposed that I need
to change something in the controller and I thought that I even tried
the suggestion you made. But that was apparently a mistake of mine
.
Thnx
See ya
Yu Co wrote:
data in the form is available under @customer.contact_detail. But after
submitting the form the contact_detail dates are not shown but the
customer dates.
Your view is expecting to find the contact_detail object in
@contact_detail, but you’re not setting this in the controller code.
Try adding:
@contact_detail = @customer.contact_detail
to your register action.
Chris