Forum: Ruby on Rails this feels ugly

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
41d423e29a5a40802aa6578654ad1102?d=identicon&s=25 travis laduke (Guest)
on 2006-02-28 07:53
(Received via mailing list)
i better explain a little to get started.
in my form for creating a Person, there's a Company field. People
belong to companies and companies have many people.

in my person form i have:
<%= text_field 'person', 'Company'  %>

in my controller i have
def create
	params['person']['Company'] = Company.find_by_name(params['person']
['Company'])
	@person = Person.new(params[:person])
	...
end

this seems ugly to me and makes me feel like this is not the best way.
I'm doing this because Person.new expects Company to be a Company
object not a string. params['person']['Company'] comes in from the
form as "cutco" or "edgeslice" or whatever.

Anyway, i'm using a text field instead of a select box, because
select boxes with more that like 3 items are annoying to use. I'm
going to autocomplete the company text field.

how do people usually create new objects (and specify who they
belong_to) from the child's (in this case Person) view/controller?


travis
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 Francois Beausoleil (Guest)
on 2006-02-28 07:56
(Received via mailing list)
2006/2/28, travis laduke <wrong@socal.rr.com>:
> in my person form i have:
> <%= text_field 'person', 'Company'  %>

You can use this instead:
<%= text_field_tag 'company' %>

def create
  params[:person][:company] = Company.find_by_name(params[:company])
  @person = Person.build(params[:person])
  if @person.save then
    ...
  else
    ...
  end
end

For your question of how we do it, that is one of the ways I do it
(auto complete and all).

Hope that helps.
This topic is locked and can not be replied to.