Create and error_messages

How should a create look like in order to get error_messages being
displayed, going back to new and show the error message? I can’t get it
working so I must miss something.

Pål Bergström wrote:

How should a create look like in order to get error_messages being
displayed, going back to new and show the error message? I can’t get it
working so I must miss something.

Could it be that I have to “turn off” RESTful somewhere?

2009/10/15 Pål Bergström [email protected]:

How should a create look like in order to get error_messages being
displayed, going back to new and show the error message? I can’t get it
working so I must miss something.

If the create fails, the controller could/should (re)render the “new”
view.

The “new” view should display there error messages.

The default behavior as exemplified by the scaffold looks something
like this in the controller:
def create
@post = Post.new(params[:post])

respond_to do |format|
  if @post.save
    flash[:notice] = 'Post was successfully created.'
    format.html { redirect_to(@post) }
    format.xml  { render :xml => @post, :status => :created,

:location => @post }
else
format.html { render :action => “new” }
format.xml { render :xml => @post.errors, :status =>
:unprocessable_entity }
end
end
end

If the save is successful, the user is redirected to the #show view.
If the save was unsuccessful, the #new view is rendered.

Notice the difference between a redirect, and a render (this took me a
little while to wrap my head around). A redirect tells the browser to
grab a completely new page (in this case the page associated with the
#show action), A render tells Rails what it should render for the
current page (in this case, the #new view).

If you look at the (standard, scaffold generated) #new view, you
should see something like:

New post

<% form_for(@post) do |f| %>
<%= f.error_messages %>

<%= f.label :title %>
<%= f.text_field :title %>

<%= f.submit 'Create' %>

<% end %>

<%= link_to ‘Back’, posts_path %>

the f.error_messages displays the error messages associated with @post.

The key here is that @post is initialized to Post.new when the #new
action is called, but it contains the (unsaved) record with the bad
fields when the #new view is rendered from the #create action.

I hope this made more sense than it confused you.

–wpd

Patrick D. wrote:

2009/10/15 P�l Bergstr�m [email protected]:

I have this view/new

<% form_tag :action => ‘create’ do %>

<%= error_messages_for ‘customer’ %>

Förnamn
<%= text_field :customer, :first_name %>

E-post
<%= text_field :customer, :email %>


<%= submit_tag 'Create' %>

<% end %>

And I have this in controller:

def new
@customer = Customer.new(params[:customer])
end

def create
@customer = Customer.new
if @customer.save
flash[:notice] = “Nytt konto skapat”
redirect_to :controller => ‘basket’
end

end

And this in model

validates_presence_of :first_name, :email

I can’t get it right. I now get “Missing template customer/create.erb in
view path app/views” when trying to save without :first_name

Pål Bergström wrote:
[…]

def create
@customer = Customer.new
if @customer.save
flash[:notice] = “Nytt konto skapat”
redirect_to :controller => ‘basket’
end

end

And this in model

validates_presence_of :first_name, :email

I can’t get it right. I now get “Missing template customer/create.erb in
view path app/views” when trying to save without :first_name

Of course. Your controller code is incorrect. Note that, in the create
action, the redirect is only called if @customer.save is true. If
validations fail, @customer.save is false, so Rails will try to render
the view of the same name as the action ( create.html.erb ). That
doesn’t exist, so you get an error.

I highly recommend using make_resourceful for your controllers. It will
make problems like this easier to avoid.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Pål Bergström wrote:

I highly recommend using make_resourceful for your controllers. It will
make problems like this easier to avoid.

What does make_resourceful mean?

On Oct 15, 2009, at 10:25 PM, Pål Bergström wrote:

Marnen Laibow-Koser wrote:

Pål Bergström wrote:

I highly recommend using make_resourceful for your controllers. It
will
make problems like this easier to avoid.

What does make_resourceful mean?

Type it into Google. You’ll be amazed how good their answer is.

I’ll save you the trouble. Here’s the project page:

Good luck.

2009/10/15 Pål Bergström [email protected]:

Pål Bergström wrote:

How should a create look like in order to get error_messages being
displayed, going back to new and show the error message? I can’t get it
working so I must miss something.

Could it be that I have to “turn off” RESTful somewhere?
No, it shouldn’t have anything to do with the RESTful-ness of you
application.

–wpd

Steve R. wrote:

On Oct 15, 2009, at 10:25 PM, P�l Bergstr�m wrote:

Marnen Laibow-Koser wrote:

P�l Bergstr�m wrote:

I highly recommend using make_resourceful for your controllers. It
will
make problems like this easier to avoid.

What does make_resourceful mean?

Type it into Google. You’ll be amazed how good their answer is.

I’ll save you the trouble. Here’s the project page:
GitHub - HamptonMakes/make_resourceful: Controller abstractor for Rails

Good luck.

Hm. I prefere to understand it instead of installing a plugin.

Don’t understand why it doesn’t work. It validates and send me back to
new, but no error message.

Pål Bergström wrote:

Does it matter, for the error_message_for to be displayed, if I use
form_tag or form_for?

Pål Bergström wrote:

Pål Bergström wrote:

Does it matter, for the error_message_for to be displayed, if I use
form_tag or form_for?

You’re completely ignoring the advice that Marnen and Patrick gave. Your
Create controller method is incorrect. You’re not handling the case when
the save method fails (due to validation errors). Re-read what Marnen
posted about what happens when @customer.save returns false, and compare
your create method with Patrick’s…