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
on 2006-03-14 02:56
on 2006-03-14 03:17
Hello Michael, 2006/3/13, Michael Gorsuch <firstname.lastname@example.org>: > 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 !
on 2006-03-14 03:35
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?
on 2006-03-14 03:44
2006/3/13, Michael Gorsuch <email@example.com>: > 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 !
on 2006-03-14 04:40
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
on 2006-03-14 04:43
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.
on 2006-03-14 08:11
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 %>
on 2006-03-14 12:54
Thanks, Peter. I'll give this a shot today.