I am brand new to rails and I am attempting to make my first web app. I have quickly run into my first problem however that I can''t seem to get by. It seems like it would be easy to get around (with my knowledge of other languages besides ruby), but I can''t seem to get by it. Right now, I have a note model that contains attributes, :title (string) :content (text) and :tags (string) In my models/note.rb file, all I have is this: class Note < ActiveRecord::Base validates_presence_of :title, :content end What I want to have done with my tag field though is a different matter. What I want to happen is that if the tag field is left blank, a place-holding "@untagged" string would be put into the :tags string. What I have attempted to do is this in the models/note.rb: if :tags.nil? :tags => ''@untagged'' end However, I get a syntax error. app/models/note.rb:3: syntax error, unexpected ''='', expecting kEND :tags = ''@untagged'' How should I go about solving this problem? Should I put this in a different part of the application to do the comparison? Thanks for your help in advance! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/29/07, telefauna <mika.adam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Right now, I have a note model that contains attributes, :title > (string) :content (text) and :tags (string) > > In my models/note.rb file, all I have is this: > > class Note < ActiveRecord::Base > validates_presence_of :title, :content > end > > What I want to have done with my tag field though is a different > matter. What I want to happen is that if the tag field is left blank, > a place-holding "@untagged" string would be put into the :tags string. > > What I have attempted to do is this in the models/note.rb: > > if :tags.nil? > :tags => ''@untagged'' > endwhere is that statement? Is that in a method? Just inserting that statement into your model without placing it in a method makes no sense. And the format for variable assignment is: my_variable = ''some value'' you normally use the => symbol when defining a hash> > However, I get a syntax error. > > app/models/note.rb:3: syntax error, unexpected ''='', expecting kEND > :tags = ''@untagged'' > > How should I go about solving this problem? Should I put this in a > different part of the application to do the comparison?you could put this into a before_save method in your model such as follows: def before_save self.tags= ''@untagged'' if self.tags.blank? end or you could alter your database schema so that the default value for tags is always ''@untagged'' using the following statement: alter table <my_table> change tags tags varchar(255) default ''@untagged'' although the first method is preferred since the second may not exactly be database agnostic and might not work in a migration either. Adam --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> where is that statement? Is that in a method? Just inserting that > statement into your model without placing it in a method makes no > sense. And the format for variable assignment is: > > my_variable = ''some value'' > > you normally use the => symbol when defining a hashI''m sorry, I forgot to say that I had the statement within a validate method. And I accidentally left in the => instead of just the = when I was playing around with the syntax, trying to get the thing to work.> you could put this into a before_save method in your model such as follows: > > def before_save > self.tags= ''@untagged'' if self.tags.blank? > endThat worked really nicely. Thanks for your help! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---