So I’ve pretty much followed the steps in the video tutorial on RoR.com
for creating a small app, to search for photos on flickr.com - all is
good, and works as intended to…
How would I go about adding validation, so if no tags are specified it
will fail. I know I have to use validates_presence_of :tags - but not
in the controller… so I tried to generate a model called ‘search’
(the form name) and added the validates_presence_of :tags - but it is
still not working.
How would I go about adding validation, so if no tags are specified it
will fail. I know I have to use validates_presence_of :tags - but not in the
controller…
The “validates_*” methods are part of ActiveRecord, so they won’t work
in
the Flickr demo at all.
You could write your own method in the controller.
In this case, you are not trying to save your information to a
database. This is a query, not a model. That means using the
validates… methods in ActiveRecord models is the wrong solution.
What you can do is to verify the information in the controller like
this:
@tags = params[:tags]
if @tags.is_empty?
flash[:notice] = “You must supply a tag”
redirect_to ‘search’
end
In short, you pull the tags out of the params array and perform your
checks there.
Actually this is not working. is_empty is not a method - and by changing
it to @tags.empty? I still cannot get it to work.
The problem with the Flickr tutorial, is that it’s using an Ajax
response,
which waxes weirdly with redirect, in my experience.
Try this:
def search
flickr = Flickr.new ‘YOUR-API-KEY’
if (@params[:tags].empty?)
render (:text => “You must supply a tag to search for”)
else
render :partial => ‘photo’, :collection =>
flickr.photos(:tags=> params[:tags], :per_page => ‘24’)
end
end
In this case, you are not trying to save your information to a
database. This is a query, not a model. That means using the
validates… methods in ActiveRecord models is the wrong solution.
What you can do is to verify the information in the controller like
this:
@tags = params[:tags]
if @tags.is_empty?
flash[:notice] = “You must supply a tag”
redirect_to ‘search’
end
In short, you pull the tags out of the params array and perform your
checks there.
def search
flickr = Flickr.new ‘YOUR-API-KEY’
if (@params[:tags].empty?)
render (:text => “You must supply a tag to search for”)
else
render :partial => ‘photo’, :collection =>
flickr.photos(:tags=> params[:tags], :per_page => ‘24’)
end
end
Works like a charm !
Thanks.
/mich
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.