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:
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
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 hash
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 hash
I'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?
end