redirect_to problem .. caching?

52 views
Skip to first unread message

Michael Mandecki

unread,
Jan 3, 2007, 12:42:21 AM1/3/07
to rubyonra...@googlegroups.com
I'm having problems with the code below.  The @photo.save seems to work (an entry is made in the database), but 'Success!' doesn't appear on the " tagging.rhtml" page unless I refresh the browser.  It looks like some sort of caching issue, but, strangely, redirect_to doesn't work no matter where I point it to.  I'm not sure what to do...can someone tell me what's going on? (and how to fix it?)

PhotosController
...
def addtag
    @person_id = params[:id].split("_")[1]
    @photo_id = params[:where]
    @photo = Photo.find(@photo_id)
    @photo.people << Person.find (@person_id)
    if @photo.save
      flash[:notice] = 'Success!'
           # redirect_to :action => 'list'
           # render :action => 'new'
    end
end

Russell Norris

unread,
Jan 3, 2007, 8:08:55 AM1/3/07
to rubyonra...@googlegroups.com
For the flash part you'll need flash.now[:notice] = "Success!" since you haven't redirected.

For the redirect_to, I suspect you should be having a double render/redirect issue since you're not telling it to redirect_to :whatevs and return. Am I right?

RSL

Russell Norris

unread,
Jan 3, 2007, 8:18:22 AM1/3/07
to rubyonra...@googlegroups.com
I should also point out that you'd only use flash.now[:notice] if you're _not_ redirecting.

RSL

On 1/3/07, Michael Mandecki < mand...@gmail.com> wrote:

MichaelM

unread,
Jan 3, 2007, 10:08:55 AM1/3/07
to Ruby on Rails: Talk
> > if @photo.save
> > flash[:notice] = 'Success!'
> > # redirect_to :action => 'list'
> > # render :action => 'new'
> > end
> > end

If I just write flash.now[:notice] = "Success!" (no redirection),
nothing happens at all. If I write flash[:notice] (no .now), then
"Success!" will show on another page if refresh my browser or even
click to another page.

Redirecting continues not to work, even written without the render or
flash statements, so it doesn't seem to be a double render/redirect.

Just to be clear, the three lines after if @photo.save are the three
statements I've tried, both together and by themselves - none work!
It's very strange, because besides being rather elementary, they're
copied from another def in the same Controller.

Anything else I can try?

MichaelM

unread,
Jan 3, 2007, 8:00:01 PM1/3/07
to Ruby on Rails: Talk
I'm pretty much ready to scrap my project. I rewrote everything that
applies to tagging, including the Controller, but no luck. In
'addtags', I even placed *just* flash[:notice] and *just* redirect_to,
but troubles persisted.

If anyone is interested, before I take down the server, I'd be glad to
show off my application. Just email me and I'll send you the link.

I've had trouble with everything from screen refresh, to documented
bugs/workarounds, to image uploading, to gem installation...it's too
much. Ruby on Rails doesn't seem ready for prime time.

Paul Corcoran

unread,
Jan 4, 2007, 12:33:48 PM1/4/07
to Ruby on Rails: Talk
Hi Michael,

DON'T GIVE UP!!!! I can tell that you are a talented programmer because
you bit off a lot for a first project. Maybe a smaller first project
getting simple to things to work and then adding to it might have saved
a bit of frustration. But I've been there as well :)

I don't know if you saw Alan's response to your "flash" problem on the
other thread. You were missing the "=" in the <%= flash[:notice] %>
statement. Using the equal sign actually inserts the text in the output
html stream. So the way you had it coded it was being executed but
never displayed. Use <% .. some code .. %> when you simply need to wrap
Ruby code in your rhtml template but not actually output it.

I'm not sure what the redirect problem is but it's not related to the
flash message. Good luck going forward and don't quit.

-Paul

MichaelM

unread,
Jan 4, 2007, 5:25:48 PM1/4/07
to Ruby on Rails: Talk
Thank you for the encouragement. Last night, I took down the server
and then thought about how else I could create my photo tagging
application. The prospect of SQL statements everywhere terrified me,
so I reinstalled everything (this time not InstantRails) and rewrote
the entire application this afternoon changing the organization a
little. [I'm a graduate student on winter break (not comp sci), so I
have the time. :]

However, I STILL have the same redirect_to and flash[:notice] problem!
The commented out statements below all work (when they're uncommented,
obviously), but neither redirect_to() or flash[:notice] works together
or alone.

def addtag
#person_id = params[:id].split("_")[1]
#photo_id = params[:where]
#photo = Photo.find(@photo_id)
#photo.people << Person.find(@person_id)
#if @photo.save
redirect_to(:controller => 'taggable', :action => 'list')
flash[:notice] = 'Great Success!'
#end
end

This is a major problem for this page, because once a tag is dropped on
a photo and the relationship is recorded in the database there needs to
be some sort of feedback (ie. 'Success!').

In response to previous suggestions, I have <%= flash[:notice] %> at
the top of my standard layout, so it's always there in every view.
Just in case, I added another <%= flash[:notice] %> in the tagging
view.

And, just as before, if I drop a tag and then click to another page or
refresh the browser, the flash[:notice] will be displayed! So strange!

Again, any suggestions would be VERY appreciated!

Long

unread,
Jan 4, 2007, 6:02:35 PM1/4/07
to rubyonra...@googlegroups.com
MichaelM wrote:
>
> However, I STILL have the same redirect_to and flash[:notice] problem!
> The commented out statements below all work (when they're uncommented,
> obviously), but neither redirect_to() or flash[:notice] works together
> or alone.
>
> def addtag
> #person_id = params[:id].split("_")[1]
> #photo_id = params[:where]
> #photo = Photo.find(@photo_id)
> #photo.people << Person.find(@person_id)
> #if @photo.save
> redirect_to(:controller => 'taggable', :action => 'list')
> flash[:notice] = 'Great Success!'
> #end
> end
>
I think the order of redirect_to follow by flash is wrong. It should be the reverse.
flash says set this message 'Great Success!' for display on the next page (redirect_to).

-- Long
http://MeandmyCity.com/ - Free, searchable business directory for local communities
http://edgesoft.ca/blog/read/2 - No-Cookie Session Support plugin for Rails

MichaelM

unread,
Jan 4, 2007, 6:16:25 PM1/4/07
to Ruby on Rails: Talk
> I think the order of redirect_to follow by flash is wrong. It should be the reverse.
> flash says set this message 'Great Success!' for display on the next page (redirect_to).

Yes, I wanted to show the various statements I've tried (in combination
together and alone). The reality is even these most basic def's won't
work:

def addtag
redirect_to(:controller => 'photos', :action => 'list')
end

-- or --

def addtag


flash[:notice] = 'Great Success!'

end

Yet 'addtag' will execute other statements, such as those required to
make a database entry.

I don't really understand how to write breakpoints and test on Rails,
but maybe someone can help me there? [Very explicitly though, because
it's new to me.]

Paul Corcoran

unread,
Jan 4, 2007, 9:59:01 PM1/4/07
to Ruby on Rails: Talk
I think I have it.

The problem is that the drag/drop support is provided via the prototype
and scriptaculous libraries. Therefore, the Ajax callback into your
photo/addtag method is expecting Javascript back. Your current
redirect, while it will work, is sending html back to the browser which
it is not expecting. That is why the flash message appears not to work
and also why the next page picks it up. Try the following:

render :update do |page|
page.redirect_to :action => 'tagging'
end

-Paul

MichaelM

unread,
Jan 4, 2007, 10:25:43 PM1/4/07
to Ruby on Rails: Talk
Yes! You're exactly right! I plugged your code into my def. Now the
page is automatically refreshed and 'Great Success!' is displayed. I'm
so glad I know what's going on!

>From here, I'm going to have the function add the name of the dropped
person/tag to a list under each picture.

Thank you Paul - and everyone - for your help!

# The working code!


def addtag
@person_id = params[:id].split("_")[1]
@photo_id = params[:where]
@photo = Photo.find(@photo_id)
@photo.people << Person.find(@person_id)

if @photo.save
flash[:notice] = "Great success!"
render :update do |page|
page.redirect_to :action => 'list'
end
end
end

Russell Norris

unread,
Jan 7, 2007, 11:28:17 AM1/7/07
to rubyonra...@googlegroups.com
I'm so glad to hear that you got things working. It'd have been a shame to have such a bad first experience with Rails taint your view of it or Ruby.

RSL

Jm Freitas

unread,
Feb 4, 2008, 8:02:22 AM2/4/08
to rubyonra...@googlegroups.com
I having the same problem.
Did you manage to find the cause or a solution to this issue?

--
Posted via http://www.ruby-forum.com/.

Brian Morearty

unread,
Jan 27, 2009, 4:50:15 PM1/27/09
to rubyonra...@googlegroups.com
Hi gang,

I had the same problem. I've just figured out the fix. Use:

redirect_to :action => :symbol

instead of

redirect_to :action => 'string'

Then the flash message is shown.

Too bad you have to remember that distinction, though. That shouldn't
be necessary.

Reply all
Reply to author
Forward
0 new messages