Michael Gorsuch
2006-Mar-14 01:54 UTC
[Rails] Single form w/ relationships: how do I integrate it?
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
Francois Beausoleil
2006-Mar-14 02:17 UTC
[Rails] Single form w/ relationships: how do I integrate it?
Hello Michael, 2006/3/13, Michael Gorsuch <michael.gorsuch@gmail.com>:> I have articles. I have categories for the articles.See an older blog entry of mine: http://blog.teksol.info/articles/2006/01/03/belongs_to-user-interface-take-ii Hope that helps ! -- Fran?ois Beausoleil http://blog.teksol.info/
Michael Gorsuch
2006-Mar-14 02:33 UTC
[Rails] Single form w/ relationships: how do I integrate it?
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 3/13/06, Francois Beausoleil <francois.beausoleil@gmail.com> wrote:> Hello Michael, > > 2006/3/13, Michael Gorsuch <michael.gorsuch@gmail.com>: > > I have articles. I have categories for the articles. > > See an older blog entry of mine: > > http://blog.teksol.info/articles/2006/01/03/belongs_to-user-interface-take-ii > > Hope that helps ! > -- > Fran?ois Beausoleil > http://blog.teksol.info/ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Francois Beausoleil
2006-Mar-14 02:42 UTC
[Rails] Single form w/ relationships: how do I integrate it?
2006/3/13, Michael Gorsuch <michael.gorsuch@gmail.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/Helpers/JavaScriptMacrosHelper.html#M000377 Hope that helps ! -- Fran?ois Beausoleil http://blog.teksol.info/
Jim Cheetham
2006-Mar-14 03:34 UTC
[Rails] Single form w/ relationships: how do I integrate it?
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 Mon, Mar 13, 2006 at 09:42:04PM -0500, Francois Beausoleil wrote:> 2006/3/13, Michael Gorsuch <michael.gorsuch@gmail.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/Helpers/JavaScriptMacrosHelper.html#M000377
Michael Gorsuch
2006-Mar-14 03:41 UTC
[Rails] Single form w/ relationships: how do I integrate it?
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 3/13/06, Jim Cheetham <jim@gonzul.net> wrote:> 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 Mon, Mar 13, 2006 at 09:42:04PM -0500, Francois Beausoleil wrote: > > 2006/3/13, Michael Gorsuch <michael.gorsuch@gmail.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/Helpers/JavaScriptMacrosHelper.html#M000377 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Peter Michaux
2006-Mar-14 07:10 UTC
[Rails] Single form w/ relationships: how do I integrate it?
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 Gorsuch
2006-Mar-14 11:51 UTC
[Rails] Single form w/ relationships: how do I integrate it?
Thanks, Peter. I''ll give this a shot today. On 3/14/06, Peter Michaux <petermichaux@gmail.com> wrote:> 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 %> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >