Reputation: 561
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 %>
Upvotes: 1
Views: 1162
Reputation: 146
...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.
Upvotes: 1
Reputation: 55829
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
Upvotes: 7