DISQUS

e168f08: Slightly updated version of the “crud” app on downloads page

  • Morris · 1 year ago
    I'm unhappy about this fix, because you're repeating yourself, and now two different methods have information about what the "new" view expects for instance variables, making the code a little fragile. I would suggest, instead of the new "#important line!", a call to the new METHOD, to pick up the necessary data, making the create method look like

    def create
    @student = Student.new(params[:student])
    if @student.save
    redirect_to :action => :index
    flash[:notice] = 'Student was successfully created.'
    else
    new #set up instance variables expected by the new view
    render :action => :new
    end
    end


    and, understanding that the reason we're doing this render instead of just redirecting to new is to preserve the value of @student, we could rewrite the second line of the new method as

    @student ||= Student.new

    to prevent overwriting the value, if it's there.
  • jgn · 1 year ago
    You can do it either way -- there is no really standard way to do this.

    Both solutions are problematic.

    If you create a new set of @sections in create, then you're not DRY. If you do "@student ||= Student.new" and call the "new' method, then your "new" method isn't really create a NEW @student in all cases, so if you think that the method name should be explanatory, then you've given up on that. Which is fine if that's what you want. I have done it both ways myself.

    In a more complicated strategy, you would "dry" up the entire new/create/edit/update cycle -- but that would require more trickery than we need right now. For pedagogical purposes -- and in this particular case -- I think the direct approach explains pretty clearly how when you get to that form, all of the data it needs must be in place.
  • jgn · 1 year ago
    Hey, one more thing following up my earlier reply:

    This is probably a good case for a private method which both new and create would leverage -- that private method would load up @sections for the drop-down.
  • picture cataloging software · 2 months ago
    thanks for the heads up, this really helped me a lot..
  • DoctorPsi · 4 months ago
    It seems that your coding is very logical and coherent, but it doesn't work as it should... Why is that?
    _______________________________
    Online Degrees
  • ouadamantite · 2 months ago
    -> @student = Student.new(params[:student]) how are you getting parameters though ?
    __________________________________________
    My erectie site.
  • web application development · 1 month ago
    Wow thanks for this really useful update. I was using the old version, but I will download the new one of course. Oh my I have spotted that this pose is written over a year ago only now. What an interesting thing - it is actually written a year ago, on 14 november 2008 :)) Nice one. However I will definitely download this application and try it out. Thanks one more time for your efforts.