I am having trouble getting error_messages_for to work.
My controller(relevant parts) looks like
def configure
@account = session[:account]
@user = User.new
end
def add_new_user_to_account
@user = User.new(params[:user])
if @user.valid?
session[:account].users << @user
end
redirect_to :action=> ''configure''
end
end
and my view configure.rhtml
<%= error_messages_for ''user'' %>
<%=start_form_tag :action
=>''add_new_user_to_account''%>
First Name: <%=text_field ''user'',
''first_name''%> <br />
Last Name: <%=text_field ''user'',
''last_name''%><br />
Username: <%=text_field ''user'',
''username''%><br />
Password: <%=password_field ''user'',
''password''%><br />
Email: <%=text_field ''user'',
''email''%><br />
Admin?<%=check_box("user", "admin")%><br />
<%=submit_tag%>
<%=end_form_tag%>
If I enter data that does not validate the record is not saved (which is
good) but error_messages_for ''user'' doesn''t show any
errors(which is
bad). If I go into the console and make an invalid user I am shown the
correct errors in @user.error so my validations are working on the
model, they just aren''t making their way through the controller to the
view.
Any ideas?
Thank you,
Matthew Margolis
blog.mattmargolis.net
On Tuesday, June 20, 2006, at 1:58 PM, Matthew Margolis wrote:>I am having trouble getting error_messages_for to work. > >My controller(relevant parts) looks like > >def configure > @account = session[:account] > @user = User.new > end > > def add_new_user_to_account > @user = User.new(params[:user]) > if @user.valid? > session[:account].users << @user > end > redirect_to :action=> ''configure'' > end > end > >and my view configure.rhtml > ><%= error_messages_for ''user'' %> ><%=start_form_tag :action =>''add_new_user_to_account''%> >First Name: <%=text_field ''user'', ''first_name''%> <br /> >Last Name: <%=text_field ''user'', ''last_name''%><br /> >Username: <%=text_field ''user'', ''username''%><br /> >Password: <%=password_field ''user'', ''password''%><br /> >Email: <%=text_field ''user'', ''email''%><br /> >Admin?<%=check_box("user", "admin")%><br /> ><%=submit_tag%> ><%=end_form_tag%> > >If I enter data that does not validate the record is not saved (which is >good) but error_messages_for ''user'' doesn''t show any errors(which is >bad). If I go into the console and make an invalid user I am shown the >correct errors in @user.error so my validations are working on the >model, they just aren''t making their way through the controller to >the view. > >Any ideas? > >Thank you, >Matthew Margolis >blog.mattmargolis.net >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsI''ve been having a similar problem lately, and I suspect that the ''redirect_to'' is swallowing up the instance variables. ''redirect_to'' will essentially make another call to the indicated action. Unless that action manually pulls the instance variables from the post data and sends them along to it''s own template, all that data is lost. As a test, try this at the beginning of the ''configure'' action.. @user = User.new(params[:user]) if params[:user] _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Kevin Olbrich wrote:> On Tuesday, June 20, 2006, at 1:58 PM, Matthew Margolis wrote: > >> I am having trouble getting error_messages_for to work. >> >> My controller(relevant parts) looks like >> >> def configure >> @account = session[:account] >> @user = User.new >> end >> >> def add_new_user_to_account >> @user = User.new(params[:user]) >> if @user.valid? >> session[:account].users << @user >> end >> redirect_to :action=> ''configure'' >> end >> end >> >> and my view configure.rhtml >> >> <%= error_messages_for ''user'' %> >> <%=start_form_tag :action =>''add_new_user_to_account''%> >> First Name: <%=text_field ''user'', ''first_name''%> <br /> >> Last Name: <%=text_field ''user'', ''last_name''%><br /> >> Username: <%=text_field ''user'', ''username''%><br /> >> Password: <%=password_field ''user'', ''password''%><br /> >> Email: <%=text_field ''user'', ''email''%><br /> >> Admin?<%=check_box("user", "admin")%><br /> >> <%=submit_tag%> >> <%=end_form_tag%> >> >> If I enter data that does not validate the record is not saved (which is >> good) but error_messages_for ''user'' doesn''t show any errors(which is >> bad). If I go into the console and make an invalid user I am shown the >> correct errors in @user.error so my validations are working on the >> model, they just aren''t making their way through the controller to >> the view. >> >> Any ideas? >> >> Thank you, >> Matthew Margolis >> blog.mattmargolis.net >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > I''ve been having a similar problem lately, and I suspect that the > ''redirect_to'' is swallowing up the instance variables. > > ''redirect_to'' will essentially make another call to the indicated > action. Unless that action manually pulls the instance variables from > the post data and sends them along to it''s own template, all that data > is lost. > > As a test, try this at the beginning of the ''configure'' action.. > > @user = User.new(params[:user]) if params[:user] > > _Kevin > >Yes, switching to render :action => ''configure'' fixed the problem. Thank you, Matthew Margolis blog.mattmargolis.net