Create model through text field help

I’m looking for the functionality described here:

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| %>

New Status Item:

<%= form.text_area
:body, :cols => ‘20’, :rows => ‘5’ %>

Select Project:
<%=
form.collection_select :project_id, Project.find(:all, :order => ‘name
ASC’), :id, :name, :prompt => “Select a Project” %>


or create one:


<%= form.text_field :new_project_name %>


<%= submit_tag “Add Status Note” %>


<%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

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 W. <rails-mailing-l…@andreas-

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…” [email protected]

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…” [email protected]

Hi Matthew

From another perspective, perhaps you may want to look at
activescaffold

On Oct 22, 8:58 pm, Matthew W. <rails-mailing-l…@andreas-

Try writing your add method like this.

def add
@note = Note.new(params[:note])
@note.user_id = session[:perm]
@note.save
end