The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Need some Ruby help

JasconiusJasconius sword criminalmad onlineRegistered User regular
edited May 2007 in Help / Advice Forum
I got a new computer, and I finally had the time to sit down and really get Ruby installed and running on my local machine and understand how to get applications set up.

Now I'm getting into the actual programming part of it.

I'm working from This tutorial which I have been following to the letter. I have my databases set up, and my scaffolding all set.

However, many pages in the application are returning errors such as the following.

In this example, it references a constant "Recipe" that I don't have any idea where it's coming from. (image in spoiler tag)
rubyerror.gif

Anyway, I have been trying to hunt it down by testing other parts of the application that work, and see how far it actually executes until it displays the error, so I know that it can create, edit, and show entries in the database just fine, but for some reason when it has to list all of them it causes that error.

Here is the source for the controller
class RecipesController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @recipes_pages, @recipes = paginate :recipes, :per_page => 10
  end

  def show
    @recipes = Recipes.find(params[:id])
  end

  def new
    @recipes = Recipes.new
  end

  def create
    @recipes = Recipes.new(params[:recipes])
    if @recipes.save
      flash[:notice] = 'Recipes was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @recipes = Recipes.find(params[:id])
  end

  def update
    @recipes = Recipes.find(params[:id])
    @recipes.date = Time.now
    if @recipes.update_attributes(params[:recipes])
      flash[:notice] = 'Recipes was successfully updated.'
      redirect_to :action => 'show', :id => @recipes
    else
      render :action => 'edit'
    end
  end

  def destroy
    Recipes.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end


@recipes_pages stands out to me as the only oddball among all of the methods, since it's the only one not in all the others (except destroy), but how do I track down the properties of @recipes_pages?

Furthermore, how did a constant like "Recipe" as opposed to "Recipes" find its way into the program in the first place?

this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

we also talk about other random shit and clown upon each other
Jasconius on

Posts

  • GeodGeod swim, swim, hungryRegistered User regular
    edited May 2007
    I did the old tutorial and it worked fine about 6 months ago, the one your doing is a bit updated. Unfortunately, that was the only experience I've had with Ruby as I found it to be slow after I started fooling around with it more. I'm trying to remember what I've done as I deleted all my source code I think, so if I think of anything I'll let you know.

    Geod on
  • JaninJanin Registered User regular
    edited May 2007
    You just learned why Rails' auto (de-)pluralization is a horribly brain-damaged idea. In your "list" method, you call "paginate :recipes". This de-pluralizes "recipes" to try to find the proper model. However, your model is actually named "Recipes", not "Recipe", so Rails falls flat on its ass.

    Your best choice would be to make all your model class names singular - "Recipe" instead of "Recipes", since that's how Rails prefers it. Any other way would be fighting against the framework.

    EDIT: in case I wasn't clear, look in /app/models, and rename Recipes.rb to Recipe.rb. Edit the file itself, and any associated database schemas, as needed. Then update your controller to call things like "Recipe.find" rather than "Recipes.find".

    Janin on
    [SIGPIC][/SIGPIC]
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited May 2007
    Yup, totally understood.

    I was wondering why the fuck the tutorial had you name your database tables in plural but scaffold them in singular... that's definitely a typo on their end.

    Thanks, I imagine that will probably solve all of my problems. Didn't know about the de-plurilization.

    Jasconius on
    this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

    we also talk about other random shit and clown upon each other
  • LewishamLewisham Registered User regular
    edited May 2007
    Jasconius wrote: »
    Yup, totally understood.

    I was wondering why the fuck the tutorial had you name your database tables in plural but scaffold them in singular... that's definitely a typo on their end.

    Thanks, I imagine that will probably solve all of my problems. Didn't know about the de-plurilization.

    The de-pluralization, as far as I am concerned, makes sense, it just forces you to do things in a certain (good) way. I keep seeing, time and time again, that when given too many options, the programmer will take a bad one and hang themselves with it. Structure rules!

    I would also recommend Why's Poignant Guide to Ruby if you want to expand your Ruby brains.

    Lewisham on
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited May 2007
    I have the Pragmatic Programmers Guide to Ruby, second edition. I wanna at least start into that before I buy another book.

    This is the first time I've had the real time to sit down and read/understand the installation procedures and how to use rails to build out the applications. So I'm just happy I was able to get something to submit something to a database, woo.

    Jasconius on
    this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

    we also talk about other random shit and clown upon each other
  • Mr.FragBaitMr.FragBait Registered User regular
    edited May 2007
    Jasconius wrote: »
    I have the Pragmatic Programmers Guide to Ruby, second edition. I wanna at least start into that before I buy another book.

    This is the first time I've had the real time to sit down and read/understand the installation procedures and how to use rails to build out the applications. So I'm just happy I was able to get something to submit something to a database, woo.

    Luckily, you don't have to buy it to read most of it.

    And what's cool, it's one of the few books that is written by someone with ADHD for those with any disorder. Not many books can claim that.

    Mr.FragBait on
Sign In or Register to comment.