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. -- Posted via http://www.ruby-forum.com/.
2009/10/15 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> 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: <h1>New post</h1> <% form_for(@post) do |f| %> <%= f.error_messages %> <p> <%= f.label :title %><br /> <%= f.text_field :title %> </p> <p> <%= f.submit ''Create'' %> </p> <% 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
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? -- Posted via http://www.ruby-forum.com/.
2009/10/15 Pål Bergström <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > 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
Patrick Doyle wrote:> 2009/10/15 P�l Bergstr�m <rails-mailing-list-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt>:I have this view/new <% form_tag :action => ''create'' do %> <%= error_messages_for ''customer'' %> <label>Förnamn</label> <%= text_field :customer, :first_name %> <label>E-post</label> <%= text_field :customer, :email %> <br /> <%= 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 -- Posted via http://www.ruby-forum.com/.
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_nameOf 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
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? -- Posted via http://www.ruby-forum.com/.
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: http://github.com/hcatlin/make_resourceful Good luck.
Steve Ross 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: > http://github.com/hcatlin/make_resourceful > > 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. -- Posted via http://www.ruby-forum.com/.
Pål Bergström wrote: Does it matter, for the error_message_for to be displayed, if I use form_tag or form_for? -- Posted via http://www.ruby-forum.com/.
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... -- Posted via http://www.ruby-forum.com/.