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. What am I doing wrong ? Regards, /mich -- Posted via http://www.ruby-forum.com/.
On 3/7/06, mich <mich@no-spam.com> wrote:> > 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060307/956f461e/attachment.html
mich wrote:> 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. > >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.
Berin Loritsch wrote:> > 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.Make sense ! Thanks ! /mich -- Posted via http://www.ruby-forum.com/.
Berin Loritsch wrote:> > @tags = params[:tags] > if @tags.is_empty? > flash[:notice] = "You must supply a tag" > redirect_to ''search'' > endActually this is not working. is_empty is not a method - and by changing it to @tags.empty? I still cannot get it to work. /mich -- Posted via http://www.ruby-forum.com/.
On 3/7/06, mich <mich@no-spam.com> wrote:> > 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060307/88ab35b9/attachment-0001.html
Joshua Wehner wrote:> 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 > endWorks like a charm ! Thanks. /mich -- Posted via http://www.ruby-forum.com/.