Hi there
Implementing my first "update" procedure worked fine.
I took this as a template for a "create" procedure, but that does not
work as expected: when I enter invalid data, the validation stops me
from saving the data, that''s working, but all fields are cleared!
Furthermore, also the data is not saved, no error message is dislayed.
Advice is more then welcome!
### Controller Code:
def create_client
case request.method
when :get
@client = Client.new
when :post
client = Client.new(params[:client])
if client.save true
flash[:notice] = "Client successfully added"
redirect_to({:controller => ''client'', :action =>
''overview''},
true)
else
flash[:errobj] = ''client''
end
end
end
### Layout Code
<% if flash[:errobj] -%>
<%= error_messages_for flash[:errobj] -%>
<% end %>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Okay, I am an idiot. But there is still hope because I found out
myself.
Instead of
client = Client.new(params[:client])
I should have said
@client = Client.new(params[:client])
............
On May 3, 7:04 am, kai
<kaithomas...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>
wrote:> Hi there
>
> Implementing my first "update" procedure worked fine.
> I took this as a template for a "create" procedure, but that does
not
> work as expected: when I enter invalid data, the validation stops me
> from saving the data, that''s working, but all fields are cleared!
>
> Furthermore, also the data is not saved, no error message is dislayed.
>
> Advice is more then welcome!
>
> ### Controller Code:
> def create_client
> case request.method
> when :get
> @client = Client.new
> when :post
> client = Client.new(params[:client])
> if client.save true
> flash[:notice] = "Client successfully added"
> redirect_to({:controller => ''client'', :action
=> ''overview''},
> true)
> else
> flash[:errobj] = ''client''
> end
> end
> end
>
> ### Layout Code
> <% if flash[:errobj] -%>
> <%= error_messages_for flash[:errobj] -%>
> <% end %>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> if client.save true > flash[:notice] = "Client successfully added" > redirect_to({:controller => ''client'', :action => ''overview''}, > true) > else > flash[:errobj] = ''client'' > end > end > end > > ### Layout Code > <% if flash[:errobj] -%> > <%= error_messages_for flash[:errobj] -%> > <% end %>If things aren''t working the way you expected, debug your code, write tests, etc. <%= debug flash[:errobj] %> <%= flash.inspect %> Or, get a little acquainted with ruby-debug and just throw a <% debugger -%> in there. Flash messages survive a request, like a redirect, but are not available in the current request. Since you just render the same template on invalid models, use flash.now[:errobj], or @errobj. -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---