I should warn that create! raises an ActiveRecord::RecordNotSaved
exception,
so unless you’re rescuing it you might want to use create instead.
I don’t understand people’s obsession with initializing an object on one
line, and then saving it on the next. There’s no point in doing this
unless
you’re changing something about the object in between!
Ok I understand that you need to write @course = Course.new
My question is I get an “only get, put, and delete requests are allowed
error” when I try to add a course from a school show page. How do I put
a form on the school show page to create a new course that links back to
the school show page once the user submits the form?
Ok I understand that you need to write @course = Course.new
My question is I get an “only get, put, and delete requests are allowed
error” when I try to add a course from a school show page. How do I put
a form on the school show page to create a new course that links back to
the school show page once the user submits the form?
I should warn that create! raises an ActiveRecord::RecordNotSaved exception,
so unless you’re rescuing it you might want to use create instead.
I don’t understand people’s obsession with initializing an object on one
line, and then saving it on the next. There’s no point in doing this unless
you’re changing something about the object in between!
I can’t claim to be obsessed with it, so I can’t give any insight into
that aspect of it, but I do use the technique, so I’ll try to explain
that part
One reason, at least, is that #create always returns the instantiated
object, whether that object got saved or not. So you can’t use it in
an “if” test (whereas you can use “if @thing.save”). You can of course
do “if @thing.valid?”, but that’s still a slightly oblique way of
determining whether or not it was saved.
David
–
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details and 2008 announcements!
Does this code go in the School -> create action??
If so how do I create a new school in the same action!? Or is it that I
should make a new action for creating a course.
Then how should my <%= form_for :course … look like in my view ?
Thanks, I’m probably just over complicating the problem, but I really
cannot figure this one out.
Ryan B. wrote:
@Ellis:
In your form_for, try :html => { :method => “put” } instead of just
:method
=> :put.
I admire your book, *Ruby for Rails: Ruby techniques for Rails
developers, *it
was the first and only Ruby/Rails book I have read cover to cover.
Taught me
a lot of interesting things.
"
One reason, at least, is that #create always returns the instantiated
object, whether that object got saved or not. So you can’t use it in
an “if” test (whereas you can use “if @thing.save”). You can of course
do “if @thing.valid?”, but that’s still a slightly oblique way of
determining whether or not it was saved."
But .create! raises an Exception. This exception could either be an
ActiveRecord::RecordNotSaved, or an ActiveRecord::RecordInvalid,
depending
on if the model does not have or has a validate method defined on it,
respectively.
I use .create! in my code, but .save with an if works just as well
def create @school = School.find(params[:school_id]) @course = @school.courses.create!(params[:course])
flash[:notice] = “Course was successfully created.”
redirect_to school_course_path(@course)
rescue ActiveRecord::NotSaved, ActiveRecord::RecordInvalid => @e
flash[:notice] = “Course could not be created.”
render :action => “new”
end
The @e instance variable will either be “ActiveRecord::NotSaved” or
whatever
errors are thrown from the validate method. I rescued both, just in
case.