Add object with REST

In a /school/1 of a restful application I would like to add a course how
is this possible?

<% form_for @course, :url => new_school_course_path(@school), :method =>
:put do |f| %>


Title

<%= f.text_field :title %>

<p>
  <b>Semester</b><br />
  <%= f.text_field :semester %>
</p>

<p>
  <b>Year</b><br />
  <%= f.text_field :year %>
</p>

<p>
  <%= f.submit "Create" %>
</p>

<% end %>

What should my school / course controller look like?

Ellis B. wrote:

In a /school/1 of a restful application I would like to add a course how
is this possible?

<% form_for @course, :url => new_school_course_path(@school), :method =>
:put do |f| %>


Title

<%= f.text_field :title %>

<p>
  <b>Semester</b><br />
  <%= f.text_field :semester %>
</p>

<p>
  <b>Year</b><br />
  <%= f.text_field :year %>
</p>

<p>
  <%= f.submit "Create" %>
</p>

<% end %>

What should my school / course controller look like?

def new
@course = Course.new(params[:course])
@course.save
end

I think that the model name is Course.
I hope this will work. You can add the codes for displaying the error or
success messages.

Karthi kn wrote:

Ellis B. wrote:

In a /school/1 of a restful application I would like to add a course how
is this possible?

<% form_for @course, :url => new_school_course_path(@school), :method =>
:put do |f| %>


Title

<%= f.text_field :title %>

<p>
  <b>Semester</b><br />
  <%= f.text_field :semester %>
</p>

<p>
  <b>Year</b><br />
  <%= f.text_field :year %>
</p>

<p>
  <%= f.submit "Create" %>
</p>

<% end %>

What should my school / course controller look like?

def new
@course = Course.new(params[:course])
@course.save
end

I think that the model name is Course.
I hope this will work. You can add the codes for displaying the error or
success messages.

Sorry. The method name is create.

def new
@course = Course.new(params[:course])
@course.save
end

Make sure to pass in a school_id too:

<%= f.hidden_field “school_id” %>

Or if you don’t want to do that, you could always get the school object
itself and just add a new course to it’s courses collection.

@school = School.find(params[:school_id])
@school.courses.create!(params[:course])

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!

On Dec 5, 2007 4:46 PM, Karthi kn [email protected]
wrote:

<p>
  <b>Year</b><br />

def new
def new
@course = Course.new(params[:course])
@course.save
end

Posted via http://www.ruby-forum.com/.


Ryan B.

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?

Ellis B. wrote:

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?

bump

Hi –

On Wed, 5 Dec 2007, Ryan B. wrote:

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 :slight_smile:

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.

The code I gave you should go in your course new view.

On Dec 6, 2007 11:41 AM, Ellis B. [email protected]
wrote:

@Ellis:

In your form_for, try :html => { :method => “put” } instead of just
:method
=> :put.


Posted via http://www.ruby-forum.com/.


Ryan B.

The thing is I’m trying to make it so that the course new view IS on the
school show view. Does this make sense?

The code I gave you should go in your course new view.

On Dec 6, 2007 11:41 AM, Ellis B. [email protected]
wrote:

@Ellis:

In your form_for, try :html => { :method => “put” } instead of just
:method
=> :put.


Posted via http://www.ruby-forum.com/.


Ryan B.

Yeah it makes sense now.

I dont think doing render :action => “school/show” would work in this
case.

Perhaps move the course new to its own page>?

On Dec 6, 2007 11:59 AM, Ellis B. [email protected]
wrote:

@Ellis:


Ryan B.


Posted via http://www.ruby-forum.com/.


Ryan B.

@Ellis:

In your form_for, try :html => { :method => “put” } instead of just
:method
=> :put.

@David:

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 :slight_smile:

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.

On Dec 6, 2007 9:40 AM, Ellis B. [email protected]
wrote:


Posted via http://www.ruby-forum.com/.


Ryan B.