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| %> <p> <b>Title</b><br /> <%= f.text_field :title %> </p> <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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ellis Berner 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| %> > <p> > <b>Title</b><br /> > <%= f.text_field :title %> > </p> > > <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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Karthi kn wrote:> Ellis Berner 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| %> >> <p> >> <b>Title</b><br /> >> <%= f.text_field :title %> >> </p> >> >> <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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Karthi kn wrote: > > Ellis Berner 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| %> > >> <p> > >> <b>Title</b><br /> > >> <%= f.text_field :title %> > >> </p> > >> > >> <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 > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Wed, 5 Dec 2007, Ryan Bigg wrote:> 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!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! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ellis Berner 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@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 :) 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 Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ellis Berner 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 > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Bigg wrote:> @Ellis: > > In your form_for, try :html => { :method => "put" } instead of just > :method > => :put. >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The code I gave you should go in your course new view. On Dec 6, 2007 11:41 AM, Ellis Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 Bigg wrote: > > @Ellis: > > > > In your form_for, try :html => { :method => "put" } instead of just > > :method > > => :put. > > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >> > @Ellis: >> > >> > In your form_for, try :html => { :method => "put" } instead of just >> > :method >> > => :put. >> > >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >> > > > -- > Ryan Bigg-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > >> > @Ellis: > >> > > >> > In your form_for, try :html => { :method => "put" } instead of just > >> > :method > >> > => :put. > >> > > >> -- > >> Posted via http://www.ruby-forum.com/. > >> > >> > > >> > > > > > > -- > > Ryan Bigg > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---