#477 ✓resolved
andyw8

In-line new form on index page causes error: undefined method `member_class'

Reported by andyw8 | August 2nd, 2009 @ 05:29 PM | in Hobo 1.0 - Final

Environment:

$ rails -v ; ruby -v ; gem list | grep 'hobo' Rails 2.2.2
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
hobo (0.8.8, 0.7.5)
hobofields (0.8.8, 0.7.5)
hobosupport (0.8.8, 0.7.5)

Steps to reproduce:

$ hobo testapp $ cd testapp $ script/generate hobo_model_resource car

Add attribute to car model: name :string
Add validation to car model: validates_presence_of :string
Set autoactions on car controller: auto_actions :all, :except => [:new]

$ script/generate hobo_migration $ script/server

Navigate to http://localhost:3000/cars

Enter 'foo' as car name.
Hit save and flash status is 'The car was created successfully'.

Navigate to http://localhost:3000/cars again
Leave car name blank and hit save.

Error is:

NoMethodError in CarsController#create

undefined method `member_class' for #

Comments and changes to this ticket

  • Bryan Larsen

    Bryan Larsen August 17th, 2009 @ 10:26 PM

    • Tag changed from defect to defect, model-controller

    nice bug report, easy to reproduce.

    I've attached a hacky fix -- I'll have to see if Tom has a better idea on how to handle it.

  • Bryan Larsen
  • Bryan Larsen

    Bryan Larsen August 18th, 2009 @ 05:43 PM

    • State changed from “new” to “resolved”
    • Milestone set to Hobo 1.0 - Final

    Here's the comment I sent to the mailing list announcing the fix:

    Hello all,

    Due to our goal of API stability, we were unable to come up with a clean fix for https://hobo.lighthouseapp.com/projects/8324-hobo/tickets/477.

    If you use auto_actions :all, :except => [:new] in your controller, Hobo very conveniently places a new-form on the default index page. This form submits to the create action. If you have validations on your model that are violated, Hobo/Rails tries re render the form with the errors highlighted. But Hobo 0.8.8 throws an exception trying to do so.

    The error occurs because the index page expects a list of records in this, but all of the other default pages expect 'this' to contain a single record.

    In our solution, we end up calling TWO different controller actions. The POST calls the #create action. If #create succeeds, it redirects to #show or whatever you've specified in :redirect as it has always done. If it fails, it now sets @invalid_record, and then calls #index so the context may be correctly populated.

    This is a general problem. If your new-page view requires extra data that your controller puts in @variables, that data will not be available when your new page is rerendered due to the validation errors.

    For 1.0, we're only fixing #index. We can justify the API change because #index was broken already. If you're running into the problem on other actions you're using custom controller actions anyways.

    Post-1.0 we'll probably extend this style of behaviour to all actions.

    You can review the patch on github:

    http://github.com/tablatom/hobo/commit/2b616a661c75341c8c7378c3be36...

    (A later patch removes the call to ruby-debug. I did run the acceptance tests on the code -- the fact that it slipped in is very strange)

    Bryan

  • Matt Jones

    Matt Jones August 18th, 2009 @ 06:04 PM

    If we reversed the order of the two terms in the definition of 'this' (in model_controller.rb), we could stick closer to Rails instance variable conventions, and keep things tidy. Instead of @invalid_record, we could have a new method, this_singular, that specifically accesses the singular instance variable. Thus, you could do this in a controller:

    class FooController < ApplicationController
      hobo_model_controller
      def index
        @foos = Foo.find(:all)
        @foo = some_particular_foo_or_a_new_record
      end
    end
    

    Then the call in DRYML would look more like:

    <form with="&this_singular || new_for_current_user(<%= model %>)" param/>
    

    I haven't figured out 100% of the plumbing on the re_render_form side, but at the very least the hobo_create action will correctly set this_singular (it calls this= with a single record).

  • johndavidd

    johndavidd November 17th, 2022 @ 07:19 AM

    It's possible that you've accidentally created a new member class on your index page when you've been using a template. When you create a new class, the new class is automatically added to the index page. If you've used a template to create your index page, the new class nyt mini crossword will be added to the template. To avoid this problem, make sure that you don't create new classes on your index page. Instead, use an in-line new form on your index page.

  • Tom Hiddleston

    Tom Hiddleston November 30th, 2022 @ 07:40 AM

    Error: undefined method `member_class' is a common mistake wordle and luckily you reported it early so it can be fixed more quickly. krunker

  • leochad82

    leochad82 December 5th, 2022 @ 08:49 AM

    The article in this forum is very good, this article is very helpful for me. Nice to read your post. If you can, take a moment to play game trap the mouse one of the most exciting games

  • Mathew23

    Mathew23 December 19th, 2022 @ 03:06 AM

    I'm attempting to use the CRUD method to create a PHP web application. Although I've come a long way, word hurdle I still can't get the code to read my most recent entries from the database.

  • theodoreevans

    theodoreevans August 10th, 2023 @ 03:48 AM

    Nice article that taught me something. This post is the best one on this important subject five nights at freddy's

  • thundernight mark
  • thundernight mark
  • dordle

    dordle January 13th, 2024 @ 04:51 AM

    After reading your post, I feel equipped to provide a few remarks or suggestions of my own. Perhaps the following article might use this one as a starting point. Please excuse me while I go look for more information on this subject. You can play: poppy playtime to relax, or pass the time!

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket

Referenced by

Pages