views:

202

answers:

2

I have this model:

class Story < ActiveRecord::Base
    validates_presence_of :name , :link
end

Where a validation for a form take place. But I want also to validate if the string "http" is included in the :link symbol. I can't do :link.include? because :link is symbol. How do I do that?

My View is this:

<% form_for :story do |f| %>
<p>
  name:<br />
  <%=f.text_field :name %>
</p>
<p>
  link:<br />
  <%= f.text_field :link %>
</p>
<p>
  <%= submit_tag :"submit this story" %>
</p>
<% end %>
+6  A: 

If you add in a validates_format_of you can supply a regex to test the link with.

class Story < ActiveRecord::Base
    validates_presence_of :name , :link
    validates_format_of :link, :with => /^http.*$/
end
Peter Coulton
Thank you very much pal.
Kevin_Jim
A: 

...in the :link symbol. I can't do :link.include? because :link is symbol.

Just so it's clear, note that :link (as a symbol) is just a way to pass the name of the link method, in this case telling the validation which methods it should work on. If you want to value returned by link (which would presumably be the link attribute of the object), you would call link as a normal method. For validations, however, Peter's answer is what you want.

autodata
Yeah, that's why I said I can't use .include? on a :symbol, it's not possible. Thank you for trying to help the helpless noob.
Kevin_Jim