Forum: Ruby on Rails Flickr - Tutorial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mich (Guest)
on 2006-03-07 18:02
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
Joshua W. (Guest)
on 2006-03-07 18:09
(Received via mailing list)
On 3/7/06, mich <removed_email_address@domain.invalid> 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.
Berin L. (Guest)
on 2006-03-07 18:19
(Received via mailing list)
mich 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.
mich (Guest)
on 2006-03-07 19:10
Berin L. 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
mich (Guest)
on 2006-03-07 19:32
Berin L. wrote:

>
> @tags = params[:tags]
> if @tags.is_empty?
>   flash[:notice] = "You must supply a tag"
>   redirect_to 'search'
> end

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.

/mich
Joshua W. (Guest)
on 2006-03-07 19:44
(Received via mailing list)
On 3/7/06, mich <removed_email_address@domain.invalid> 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
mich (Guest)
on 2006-03-08 11:27
Joshua W. 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
> end

Works like a charm !

Thanks.

/mich
This topic is locked and can not be replied to.