Forum: Ruby on Rails Single form w/ relationships: how do I integrate it?

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.
Michael G. (Guest)
on 2006-03-14 03:56
(Received via mailing list)
OK - I know this has been asked before, but I cannot find it in the
archives.  Forgive me if I'm creating more noise than I should.

I want to know the easiest way to deal with this scenario:

I have articles.  I have categories for the articles.

On the form where you write an article, there needs to be a free-form
field to entire the category.

The create() method will build the article, find_or_create_by_name the
category, and assign it to the article.  After that, the article is
saved.

I need to display an error if the category isn't included, and I need
it to act like a normal form error: add its message to the list of
errors, and highlight the field.

Does anyone know an easy way to do this?  I know it's right here in
front of me...

Thanks in advance,

Michael
François B. (Guest)
on 2006-03-14 04:17
(Received via mailing list)
Hello Michael,

2006/3/13, Michael G. <removed_email_address@domain.invalid>:
> I have articles.  I have categories for the articles.

See an older blog entry of mine:

http://blog.teksol.info/articles/2006/01/03/belong...

Hope that helps !
Michael G. (Guest)
on 2006-03-14 04:35
(Received via mailing list)
That's a great article, and I have used that technique before.

My question is this: how do I do the same, but with a 'text_field'?  I
need to allow the user to enter in whatever he or she likes, but it
must be something.

Any ideas how to accomplish this?
François B. (Guest)
on 2006-03-14 04:44
(Received via mailing list)
2006/3/13, Michael G. <removed_email_address@domain.invalid>:
> That's a great article, and I have used that technique before.
>
> My question is this: how do I do the same, but with a 'text_field'?  I
> need to allow the user to enter in whatever he or she likes, but it
> must be something.
>
> Any ideas how to accomplish this?

Oh, sorry.  I hadn't read your message properly.

You probably want to use a text_field_with_auto_complete:

http://api.rubyonrails.com/classes/ActionView/Help...

Hope that helps !
Jim C. (Guest)
on 2006-03-14 05:40
(Received via mailing list)
I'd go one further - I'd like the user to autocomplete, but to also be
allowed to invent a new entry for 'color' at the same time as entering a
new product ...

How would that work?

-jim
Michael G. (Guest)
on 2006-03-14 05:43
(Received via mailing list)
I'm in the same spot, Jim.

I'm thinking that using the 'text_field_with_autocomplete' coupled
with a 'find_or_create_by_name' combination would work.

Haven't tried it yet, though.
Peter M. (Guest)
on 2006-03-14 09:11
(Received via mailing list)
I've done something I think is similar so I'll take a stab. I haven't
used find_or_create_by so there might be a better way if I knew what
it returns in the case of errors. Seems like the way i used
find_or_create_by below makes the whole thing bulky. Anyway this might
spark an idea.

The line below

 if @category.name.errors.on(:name)

is very weak maybe you can write

 if @category.name.errors.length

or something better



> I need to display an error if the category isn't included, and I need
> it to act like a normal form error: add its message to the list of
> errors, and highlight the field.

In the models

class Category < ActiveRecord::Base
  validates_presence_of :name
  has_many :articles
end

class Article < ActiveRecord::Base
  validates_presence_of :the_article_text
  belongs_to :category
end


In the controller action

def new_article

  if params[:submit]
    @category=Category.find_or_create_by_name(params[:category])
    @article=Article.new(params[:article])
    if @category.name.errors.on(:name)
      render
      return
    else
       @article.category_id = @category.id
       unless @article.save
             render
             return
       end

       redirect_to :action=>"somewhere"
       return
    end
  end

  @category = Category.new
  @article = Article.new
end

In new new_article.rhtml view

<%= form_tag %>
<%= text_field :category, :name %>
<%= text_area :article, :the_article_text %>
<%= submit_tag %>
<%= end_form_tag %>
Michael G. (Guest)
on 2006-03-14 13:54
(Received via mailing list)
Thanks, Peter.

I'll give this a shot today.
This topic is locked and can not be replied to.