Are the default source files best practice?
Hey, I have some experience with MVC. but i am new to rails. I am using the scaffold command to generate some default files. The model looks clean and beautiful, but the controller and views aren't really dry. The content of new.html.erb and edit.html.erb is almost the same, and the new / edit and create / update methods do much the same. In other frameworks, I only used one view to update and create new records, and also the same method in my controller, setting id as an optional parameter. Do they use this structure to keep things RESTful (I don't really understand the essence of rest :()? Is it better to use this default stuff for crud?
source to share
The scaffold generator is a pretty good place to start. As you pointed out, there are some things that are not that good at this. I think most people take what the scaffold creates and then fix it to their liking. For example, you can extract a portion of a form from new.html.erb
and edit.html.erb
and place it in a partial _form.html.erb
. Then update new.html.erb
and edit.html.erb
to include this partial display of the form. I think for Rails 3 the scaffold generator has been modified to make this the default.
It seems like new
and edit
, and, create
and update
are pretty much the same, but you need to remember that they map to various HTTP methods and URLs that relate to the whole idea of a RESTful resource. Check out RailsGuides for routing, the CRUD, Verbs & Actions section has a nice table of seven different routes and the differences between them.
source to share
You should check out ryanb nifty-generator:
http://github.com/ryanb/nifty-generators
Scaffolding creates a partial view called _form
, which is then referenced by views new
and edit
. It also includes many other nice options - for example, creating your views in haml or your tests in Shoulda or RSpec.
source to share