How can I take the advantage of scaffolding?

I am creating a site which just displays comments and person_detail.
I am thinking:

  • Create 2 models: comments & personal_detail corresponding to 2 tables.
  • 2 controllers: comments_controller & personal_detail_controller.
  • Create a controller “home”, which invokes actions provided by
    comments_controller & personal_detail_controller, then display info in
    the home.rhtml.

I wonder how scaffolding can simplify the tasks.

Gary Liang wrote:

I am creating a site which just displays comments and person_detail.
I am thinking:

  • Create 2 models: comments & personal_detail corresponding to 2 tables.
  • 2 controllers: comments_controller & personal_detail_controller.
  • Create a controller “home”, which invokes actions provided by
    comments_controller & personal_detail_controller, then display info in
    the home.rhtml.

I wonder how scaffolding can simplify the tasks.

I also think I can just create a home_controller & 2 models(comments &
personal_detail). I am able to display via the actions provied by those
2 models, right?

I think it would be simpler to stick with the design with a controller
for each model (plus a HomeController if you need it). It’s much easier
to go with the CRUD method names created automatically by the
scaffolding in each controller, than to have one overall controller that
handles lots of methods that deal with different models.

Shauna

Gary Liang wrote:

Someone may ask this question. How can I call actions from other
controllers in a overall controller?

I’m not completely sure what you’re asking… Perhaps inheritance?
Basically, can a controller call methods from another controller? The
short answer is yes.

The most logical way would be to use inheritance (I say logical because
this is Ruby and you can technically call almost anything from
anywhere). Imagine you have an admin controller with several children
controllers. E.g…

class AdminController < ApplicationController

def admin_method

end

end

class UsersController < AdminController

def users_method

admin_method

end

end

I’d advise you think through your problem space and ask yourself if you
really need to do such a thing. Perhaps some of the responsibility
should be assigned to your models?

Daniel W. wrote:

Gary Liang wrote:

Someone may ask this question. How can I call actions from other
controllers in a overall controller?

I’m not completely sure what you’re asking… Perhaps inheritance?
Basically, can a controller call methods from another controller? The
short answer is yes.

The most logical way would be to use inheritance (I say logical because
this is Ruby and you can technically call almost anything from
anywhere). Imagine you have an admin controller with several children
controllers. E.g…

class AdminController < ApplicationController

def admin_method

end

end

class UsersController < AdminController

def users_method

admin_method

end

end

I’d advise you think through your problem space and ask yourself if you
really need to do such a thing. Perhaps some of the responsibility
should be assigned to your models?

e.g.

class AdminController < ApplicationController
UsersController usersController = new …
usersController.action

end

class UsersController < ApplicationController

end

How RoR handle this?

Gary Liang wrote:

e.g.

class AdminController < ApplicationController
UsersController usersController = new …
usersController.action

end

class UsersController < ApplicationController

end

How RoR handle this?

Look into redirect_to, e.g.
redirect_to(:controller => ‘loans’, :action => ‘list’)

Shauna

Shauna wrote:

Gary Liang wrote:

e.g.

class AdminController < ApplicationController
UsersController usersController = new …
usersController.action

end

class UsersController < ApplicationController

end

How RoR handle this?

Look into redirect_to, e.g.
redirect_to(:controller => ‘loans’, :action => ‘list’)

Shauna

Thx Shauna. I think redirect_to will trasfer you to another controller,
then execute the list action, then stay in loans.rhtml, perhaps?

If that is the case, it may not be what I want. The idea is to stay at
the same page, but it has ability to access vary actions from other
controllers. I am not sure how RoR handle this?

If that is the case, it may not be what I want. The idea is to stay at
the same page, but it has ability to access vary actions from other
controllers. I am not sure how RoR handle this?

Sound to me like you either:

a) Need to push as much logic into the models as you can (see
Buckblog: Skinny Controller, Fat Model)

or

b) Want to implement AJAX on the client.

Shauna wrote:

I think it would be simpler to stick with the design with a controller
for each model (plus a HomeController if you need it). It’s much easier
to go with the CRUD method names created automatically by the
scaffolding in each controller, than to have one overall controller that
handles lots of methods that deal with different models.

Shauna

Someone may ask this question. How can I call actions from other
controllers in a overall controller?

Andrew S. wrote:

If that is the case, it may not be what I want. The idea is to stay at
the same page, but it has ability to access vary actions from other
controllers. I am not sure how RoR handle this?

Sound to me like you either:

a) Need to push as much logic into the models as you can (see
Buckblog: Skinny Controller, Fat Model)

or

b) Want to implement AJAX on the client.

class Whatever
def self.action

end
end

In this case, I can call Whatever.action inside any controller. That is
what I want.

e.g.

  • ruby script/generate scaffolding Users

  • Create a controller ‘Admin’

  • class Users

    def self.list
    list
    end

    def list
    …generate by scaffolding…
    end

    end

  • class Admin

    def list
    Users.list
    end

    end

  • Inside the Admin.rhtml

    @admin = Admin.new
    @admin.list

I don’t have a good feeling about this code…

Gary Liang wrote:

class UsersController < ApplicationController

end

How RoR handle this?

You can do that by doing something like this:

class UsersController < ApplicationController

def user_method
end

end

class AdminController < ApplicationController

def admin_method
user = UsersController.new
retvalue = user.user_method
end

end

but generally I believe the recommended way for “sharing” methods
between controllers is to put stuff in application.rb or create a module
and mix that in to the controllers that need it.


Michael W.

Gary Liang wrote:

Gary Liang wrote:

I am creating a site which just displays comments and person_detail.
I am thinking:

  • Create 2 models: comments & personal_detail corresponding to 2 tables.
  • 2 controllers: comments_controller & personal_detail_controller.
  • Create a controller “home”, which invokes actions provided by
    comments_controller & personal_detail_controller, then display info in
    the home.rhtml.

I wonder how scaffolding can simplify the tasks.

I also think I can just create a home_controller & 2 models(comments &
personal_detail). I am able to display via the actions provied by those
2 models, right?

Im a newbie but you dont have controllers for both models you will have
to do a lot of rediect_to 's with hardcoded contoller names and actions
keep track
of data/view flows.

It will be easier to discuss solutions if you give us some more details
about how you want your app to behave (rather than picking a particular
syntax and asking if that syntax can do this or that aspect of what you
want).

Shauna