How to reload/redirect in custom action?

0 views
Skip to first unread message

cschlaefcke

unread,
Mar 8, 2007, 11:01:36 AM3/8/07
to Ruby On Rails: Ajax Scaffold
Hi Folks,

I really love this ajax scaffold plugin! It works great with some of
my simpler examples.
But now I want to do something bejond those simple examples and stuck
with some problems.

1. Implement a toggle link (e.g Select/Unselect) on each row in a list
- I have overriden _row.rhtml to add a new action "Select" in the
actions area of the list. Works!
<td>
<% select_label = (is_row_selected(row.id) ?
"Unselect":"Select") %>
<% select_options = @options.merge(:action =>
'select_row'+@suffix) %>
<%= link_to_remote select_label,
{ :url => select_options },
{ :href => url_for( select_options ) } %>
</td>

- I have added a new action in my controller that gets triggered by
the select action. Works!
def select_row
row_id = params['id'].to_i

if session[:selected_row]
if session[:selected_row].to_i == row_id.to_i
session[:selected_row] = nil
else
session[:selected_row] = row_id
end
else
session[:selected_row] = row_id
end

redirect_to :action => "list"
end

The basic function should be like this now:
Hit "Select" on a certain row => Store row_id to session, change
action label to "Unselect"
Hit "Select" on another row =>
Store new row_id to session, change action label to "Unselect".
Change old to "Select"
Hit "Unselect" on selected row => remove row_id from session. Change
label to "Select"

When I hit the "Select" action on a certain row I can see from my
debugging, that the action "select_row" gets called. So the basic
functionality is working.

My Problem: The label only toggles when I manually hit "Reload". The
redirect_to does not seem to work. So what´s wrong?

2. Redirect to another conroller/action from a custom action
What I want to do next is to provide a certain action on the selected
row (e.g. analyse). This should take the data from the selected row
and compute some new data in another table with it.
So when I hit the analyse action in my view the processing should
start and after that I want the user to get redirected to the new page
with the results of the different table.

I have this in my controller:
def analyse
row_id = session[:selected_row]
print "Going to analyse row #{row_id}...\n"
# Do the analyse and store results elsewhere ...
print "...done!\n"

redirect_to :controller => "result", :action => "list")
end

And again: The action gets called perfectly but the redirect_to is not
taking me to the results page. What´s wrong here?

I have the feeling that I have general misunderstanding on this
redirecting thing in combination with ajax scaffold. I tried it the
same way I would have used with plain rails (which worked perfectly in
different examples). Maybe there is another approach on how this
should be done.
Hmmpf, it´s quite dark in here, could somebody please bring in some
light for me? :-)

Thanks & Regards,

Christian

Lance Ivy

unread,
Mar 9, 2007, 12:59:31 PM3/9/07
to ajaxsc...@googlegroups.com
Hi Christian,

I don't think that redirect_to will work when the client wants a javascript-based response. Try something like:

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


On 3/8/07, cschlaefcke <cschl...@wms-network.de> wrote:

Hi Folks,

I really love this ajax scaffold plugin! It works great with some of
my simpler examples.
But now I want to do something bejond those simple examples and stuck
with some problems.

1. Implement a toggle link ( e.g Select/Unselect) on each row in a list

- I have overriden _row.rhtml to add a new action "Select" in the
actions area of the list. Works!
          <td>
            <% select_label = (is_row_selected( row.id) ?
Reply all
Reply to author
Forward
0 new messages