I''m looking for the functionality described here: http://railscasts.com/episodes/57 My application is very simple. I have a note model and a project model. A note belongs to a project and a project has many notes. When I create a note I would like the option to select an existing project or to create a new one. What''s not explained in the Railscast (I''m unable to watch it now to verify but when I did watch it I didn''t hear an explanation) is how to handle the form in your controller. Some code: Note model: # Associations belongs_to :project attr_accessor :new_project_name before_save :create_project_from_name def create_project_from_name create_project(:name => new_project_name) unless new_project_name.blank? end View: <% form_for :note, :url => { :action => :add } do |form| %> <strong><p>New Status Item:</p></strong> <%= form.text_area :body, :cols => ''20'', :rows => ''5'' %> <p><strong>Select Project:<br></strong><%form.collection_select :project_id, Project.find(:all, :order => ''name ASC''), :id, :name, :prompt => "Select a Project" %></p> <p>or create one:</p> <p><%= form.text_field :new_project_name %></p> <p><%= submit_tag "Add Status Note" %></p> <%end%> Controller: def add # Create a Note object with data from the submitted form @body = params[:note][:body] @user_id = session[:perm] @project_id = # Should be the ID of the selected project or the ID of the newly created @note = Note.create(:body => @body, :project_id => @project_id, :user_id => @user_id, :project_id => @project_id) end As you can see I don''t have any code to determine how to determine the correct ID of the project when I write the note to the database. My add action could also be built incorrectly with the methodology involved, it works otherwise but I don''t know if it''s the best practice for handling form data... Any comments on my code would welcomed as well. Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
You don''t need to specify the project_id, this is done for you by using the create_project method that was provided by the association. Just run the create without the project_id and it should work. On Oct 18, 1:32 pm, Matthew Williams <rails-mailing-l...@andreas- s.net> wrote:> I''m looking for the functionality described here:http://railscasts.com/episodes/57 > > My application is very simple. > > I have a note model and a project model. A note belongs to a project > and a project has many notes. When I create a note I would like the > option to select an existing project or to create a new one. What''s not > explained in the Railscast (I''m unable to watch it now to verify but > when I did watch it I didn''t hear an explanation) is how to handle the > form in your controller. > > Some code: > > Note model: > # Associations > belongs_to :project > > attr_accessor :new_project_name > before_save :create_project_from_name > > def create_project_from_name > create_project(:name => new_project_name) unless > new_project_name.blank? > end > > View: > <% form_for :note, :url => { :action => :add } do |form| %> > <strong><p>New Status Item:</p></strong> <%= form.text_area > :body, :cols => ''20'', :rows => ''5'' %> > <p><strong>Select Project:<br></strong><%> form.collection_select :project_id, Project.find(:all, :order => ''name > ASC''), :id, :name, :prompt => "Select a Project" %></p> > <p>or create one:</p> > <p><%= form.text_field :new_project_name %></p> > <p><%= submit_tag "Add Status Note" %></p> > <%end%> > > Controller: > def add > # Create a Note object with data from the submitted form > @body = params[:note][:body] > @user_id = session[:perm] > @project_id = # Should be the ID of the selected project or the ID > of the newly created > @note = Note.create(:body => @body, > :project_id => @project_id, > :user_id => @user_id, > :project_id => @project_id) > > end > > As you can see I don''t have any code to determine how to determine the > correct ID of the project when I write the note to the database. My add > action could also be built incorrectly with the methodology involved, it > works otherwise but I don''t know if it''s the best practice for handling > form data... Any comments on my code would welcomed as well. > > Thanks > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
You could do it like this but its not necessary. def create_project_from_name new_project = create_project(:name => new_project_name) unless new_project_name.blank? self.project_id = new_project.id end Unless you were going to check for the new projects existance first like this. def create_project_from_name if project = Project.find(:first, :conditions => {:name => new_project_name}) then self.project_id = project.id else create_project(:name => new_project_name) unless new_project_name.blank? end end On Oct 19, 1:54 pm, "randomutterings..." <randomutteri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You don''t need to specify the project_id, this is done for you by > using the create_project method that was provided by the association. > Just run the create without the project_id and it should work. > > On Oct 18, 1:32 pm, Matthew Williams <rails-mailing-l...@andreas- > > s.net> wrote: > > I''m looking for the functionality described here:http://railscasts.com/episodes/57 > > > My application is very simple. > > > I have a note model and a project model. A note belongs to a project > > and a project has many notes. When I create a note I would like the > > option to select an existing project or to create a new one. What''s not > > explained in the Railscast (I''m unable to watch it now to verify but > > when I did watch it I didn''t hear an explanation) is how to handle the > > form in your controller. > > > Some code: > > > Note model: > > # Associations > > belongs_to :project > > > attr_accessor :new_project_name > > before_save :create_project_from_name > > > def create_project_from_name > > create_project(:name => new_project_name) unless > > new_project_name.blank? > > end > > > View: > > <% form_for :note, :url => { :action => :add } do |form| %> > > <strong><p>New Status Item:</p></strong> <%= form.text_area > > :body, :cols => ''20'', :rows => ''5'' %> > > <p><strong>Select Project:<br></strong><%> > form.collection_select :project_id, Project.find(:all, :order => ''name > > ASC''), :id, :name, :prompt => "Select a Project" %></p> > > <p>or create one:</p> > > <p><%= form.text_field :new_project_name %></p> > > <p><%= submit_tag "Add Status Note" %></p> > > <%end%> > > > Controller: > > def add > > # Create a Note object with data from the submitted form > > @body = params[:note][:body] > > @user_id = session[:perm] > > @project_id = # Should be the ID of the selected project or the ID > > of the newly created > > @note = Note.create(:body => @body, > > :project_id => @project_id, > > :user_id => @user_id, > > :project_id => @project_id) > > > end > > > As you can see I don''t have any code to determine how to determine the > > correct ID of the project when I write the note to the database. My add > > action could also be built incorrectly with the methodology involved, it > > works otherwise but I don''t know if it''s the best practice for handling > > form data... Any comments on my code would welcomed as well. > > > Thanks > > -- > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Should I not be creating my record from the form data inside my controller? If I eliminate the project_id from the create then I get a nil received error so it''s clearly not getting the project_id from the created project. randomutterings... wrote:> You could do it like this but its not necessary. > > def create_project_from_name > new_project = create_project(:name => new_project_name) unless > new_project_name.blank? > self.project_id = new_project.id > end > > Unless you were going to check for the new projects existance first > like this. > > def create_project_from_name > if project = Project.find(:first, :conditions => {:name => > new_project_name}) then > self.project_id = project.id > else > create_project(:name => new_project_name) unless > new_project_name.blank? > end > end > > On Oct 19, 1:54 pm, "randomutterings..." <randomutteri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>-- 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 -~----------~----~----~----~------~----~------~--~---
Hi Matthew>From another perspective, perhaps you may want to look atactivescaffold On Oct 22, 8:58 pm, Matthew Williams <rails-mailing-l...@andreas- s.net> wrote:> Should I not be creating my record from the form data inside my > controller? > > If I eliminate the project_id from the create then I get a nil received > error so it''s clearly not getting the project_id from the created > project. > > > > > > randomutterings... wrote: > > You could do it like this but its not necessary. > > > def create_project_from_name > > new_project = create_project(:name => new_project_name) unless > > new_project_name.blank? > > self.project_id = new_project.id > > end > > > Unless you were going to check for the new projects existance first > > like this. > > > def create_project_from_name > > if project = Project.find(:first, :conditions => {:name => > > new_project_name}) then > > self.project_id = project.id > > else > > create_project(:name => new_project_name) unless > > new_project_name.blank? > > end > > end > > > On Oct 19, 1:54 pm, "randomutterings..." <randomutteri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > -- > Posted viahttp://www.ruby-forum.com/.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try writing your add method like this. def add @note = Note.new(params[:note]) @note.user_id = session[:perm] @note.save end On Oct 22, 9:08 am, CCH <c...-Z/Eug3sLCsSPzdAsCNHSEg@public.gmane.org> wrote:> Hi Matthew > > >From another perspective, perhaps you may want to look at > > activescaffold > > On Oct 22, 8:58 pm, Matthew Williams <rails-mailing-l...@andreas- > > s.net> wrote: > > Should I not be creating my record from the form data inside my > > controller? > > > If I eliminate the project_id from the create then I get a nil received > > error so it''s clearly not getting the project_id from the created > > project. > > > randomutterings... wrote: > > > You could do it like this but its not necessary. > > > > def create_project_from_name > > > new_project = create_project(:name => new_project_name) unless > > > new_project_name.blank? > > > self.project_id = new_project.id > > > end > > > > Unless you were going to check for the new projects existance first > > > like this. > > > > def create_project_from_name > > > if project = Project.find(:first, :conditions => {:name => > > > new_project_name}) then > > > self.project_id = project.id > > > else > > > create_project(:name => new_project_name) unless > > > new_project_name.blank? > > > end > > > end > > > > On Oct 19, 1:54 pm, "randomutterings..." <randomutteri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > -- > > Posted viahttp://www.ruby-forum.com/.-Hide quoted text - > > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---