# Create model through text field help

**URL:** <https://www.ruby-forum.com/t/create-model-through-text-field-help/117630>\
**Category:** Rails\
**Created:** [October 18, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/create-model-through-text-field-help/117630 "2007-10-18T19:32:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 18, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/create-model-through-text-field-help/117630/1 "2007-10-18T19:32:14Z")

</div>

I’m looking for the functionality described here:

> **[\#57 Create Model Through Text Field - RailsCasts](http://railscasts.com/episodes/57-create-model-through-text-field)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

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

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 19, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/create-model-through-text-field-help/117630/2 "2007-10-19T19:54:33Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 19, 2007, 9:43pm UTC](https://www.ruby-forum.com/t/create-model-through-text-field-help/117630/3 "2007-10-19T21:43:27Z")

</div>

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…” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 22, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/create-model-through-text-field-help/117630/4 "2007-10-22T14:58:24Z")

</div>

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…” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 22, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/create-model-through-text-field-help/117630/5 "2007-10-22T15:09:01Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 23, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/create-model-through-text-field-help/117630/6 "2007-10-23T16:01:12Z")

</div>

Try writing your add method like this.

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