On Mon, Dec 29, 2008 at 12:13 PM, Sarah Allen <
rails-mailing-l...@andreas-s.net> wrote:

>
> I'm new to Rails and I'm trying to understand the code created by
> generate scaffold.  I'm using Rails 2.2.2.
>
> $ ./script/generate scaffold Task description:string
>
> generated the following code in app/controller/tasks_controller.rb
>
> class TasksController < ApplicationController
>  # GET /tasks
>  # GET /tasks.xml
>  def index
>    @tasks = Task.find(:all)
>
>    respond_to do |format|
>      format.html # index.html.erb
>      format.xml  { render :xml => @tasks }
>    end
>  end
>
>  # GET /tasks/1
>  # GET /tasks/1.xml
>  def show
>    @task = Task.find(params[:id])
>
>    respond_to do |format|
>      format.html # show.html.erb
>      format.xml  { render :xml => @task }
>    end
>  end
>
>  # GET /tasks/new
>  # GET /tasks/new.xml
>  def new
>    @task = Task.new
>
>    respond_to do |format|
>      format.html # new.html.erb
>      format.xml  { render :xml => @task }
>    end
>  end
>
>  # GET /tasks/1/edit
>  def edit
>    @task = Task.find(params[:id])
>  end
>
> I noticed that the edit method is missing some code.  If I remove those
> same lines from each of the methods above.  The code still works, why?
>
> I couldn't find ApplicationController in the API docs at
> http://api.rubyonrails.org/ ... I figured the behavior must be defined
> in the super class, but I can't seem to find the code for it either.
>
> Can someone point me in the right direction to illuminate this mystery?
>
> Thanks in advance,
> Sarah
>
Try creating a task, and then editing it.  You should find that, when you
remove the lines from "Edit", you don't get information that you previously
entered when you created the task.

If you look in app/controllers/application.rb, you will find that
ApplicationController is a task that you derived from
ActionController::Base.  You should be able to find documentation about
ActionController::Base at http://api.rubyonrails.org/

>From one newbie to another...
Look at the links created in the index view for your tasks controller.  With
the default scaffold, you should see three links to the right of each task
listed: Show, Edit, and Destroy.  If you mouse-over the Edit link, you will
probably see something that looks like: http://localhost:3000/tasks/1/edit.
That says to the routing system in Rails (through the magic of the code you
wrote in config/routes.rb) "Invoke the 'edit' action in the 'tasks'
controller, and, by the way, set the 'id' parameter to '1'".

When your #edit action is invoked, it searches the Task table in your
database for the entry with an ID of 1.  Then Rails, by default, will render
a view (a web page) named "edit".  You can see the template for that web
page in app/views/edit.html.erb.

If you completely remove the #edit action, the default behavior is still to
render a template with the same name as the action.

If you leave the #edit action in there, but don't do anything in it, the
default behavior is still to render a template with the same name as the
action.

If you leave the code in there as written, the #edit action will fetch the
row from the Task database with the specified ID.  The #edit view will then
have some data to show when it displays the form.

--wpd

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to