Render action from post call

Hi,

I am using the link_to_remote helper to call an action using and XMLHttp
request. The action method has a call to render another action if a
condition is met:

if session[:prebooking].start_date != nil
render :action => ‘check_rate’
end

the condition is being met, i.e. I checked that
‘session[:prebooking].start_date != nil’ is in fact true, but program
fails to render ‘check_rate’.

Is this because I am using a HTTP post request to call the action? How
do I get ‘check_rate’ to render if the condition is met?

Thank you!

Sam

if session[:prebooking].start_date != nil
render :action => ‘check_rate’
end

Just checking - you dont have code under the if statement that renders
something else?

Only ask because I’ve done something similar before now ;0)

Luke

Luke P. wrote:

if session[:prebooking].start_date != nil
render :action => ‘check_rate’
end

Just checking - you dont have code under the if statement that renders
something else?

Only ask because I’ve done something similar before now ;0)

Luke

No, this is the end of the method:

if session[:prebooking].start_date != nil
    render :action => 'check_rate'
end

end

Sam

Sam W. wrote:

Luke P. wrote:

if session[:prebooking].start_date != nil
render :action => ‘check_rate’
end

Just checking - you dont have code under the if statement that renders
something else?

Only ask because I’ve done something similar before now ;0)

Luke

No, this is the end of the method:

if session[:prebooking].start_date != nil
    render :action => 'check_rate'
end

end

Sam

Take your render call out of the “if” block and see what happens. If it
renders what you expect, the bug is in your condition, not the
rendering.

Alex W. wrote:

Sam W. wrote:

Luke P. wrote:

if session[:prebooking].start_date != nil
render :action => ‘check_rate’
end

Just checking - you dont have code under the if statement that renders
something else?

Only ask because I’ve done something similar before now ;0)

Luke

No, this is the end of the method:

if session[:prebooking].start_date != nil
    render :action => 'check_rate'
end

end

Sam

Take your render call out of the “if” block and see what happens. If it
renders what you expect, the bug is in your condition, not the
rendering.

I already tested the condition by doing this:

if session[:prebooking].start_date != nil
puts “condition met”
render :action => ‘check_rate’
end

This puts “condition met” at the console.

Sam

Sam W. wrote:

Alex W. wrote:

Sam W. wrote:
Take your render call out of the “if” block and see what happens. If it
renders what you expect, the bug is in your condition, not the
rendering.

I already tested the condition by doing this:

if session[:prebooking].start_date != nil
puts “condition met”
render :action => ‘check_rate’
end

This puts “condition met” at the console.

I think what he means is that maybe the condition is good, but the
render is bad. For example, do you have an :update in your
link_to_remote? Maybe the rendering is just getting thrown away.

Jake

Jake J. wrote:

Sam W. wrote:

Alex W. wrote:

Sam W. wrote:
Take your render call out of the “if” block and see what happens. If it
renders what you expect, the bug is in your condition, not the
rendering.

I already tested the condition by doing this:

if session[:prebooking].start_date != nil
puts “condition met”
render :action => ‘check_rate’
end

This puts “condition met” at the console.

I think what he means is that maybe the condition is good, but the
render is bad. For example, do you have an :update in your
link_to_remote? Maybe the rendering is just getting thrown away.

Jake

I don’t have an :update. What should I store in this value because I
don’t want to update a partial, I want to render a different action if
this specific condition is met.

Sam

Since you don’t have an :update, the browser assumes what you have
rendered is javascript (btw I find FireBug absolutely essential for
doing this kind of thing)

You can’t quite do what you want to do with link to remote.

With link to remote you either :
-generate some javascript (via rjs or render :update) that is run when
the response is returned to the browser

  • include an :update parameter, the content of the specified element on
    the page is replaced with whatever you render

There isn’t a specific replace the whole page - although you could of
course pass an :update that effectively does that.

Fred

Sam W. wrote:

Here’s what I came up with:

<%= link_to_remote @current_two_months[0][days_in_first_week].date.mday,
:url => {:action => ‘start_date’, :id => day}, :update => “month” if
@current_two_months[0][days_in_first_week].date.wday == i &&
session[:prebooking].end_date == nil%>

<%= link_to @current_two_months[0][days_in_first_week].date.mday,
{:action => ‘check_rate’, :id => day} if
@current_two_months[0][days_in_first_week].date.wday == i &&
session[:prebooking].end_date != nil%>

This way a XMLHttp post link is created if the user has not already
selected an end_date for their vacation stay. However, if the user has
already selected the end_date using javascript on the second calendar,
the application will move forward in the process using a standard get
link and vise versa. By the way, after updating the session data, with
either the start_date or end date, the link_to_remote method is set to
update the months partial so that the the AJAX links are replaced with
get requests.

Best Regards,

Sam W.

New, improved and tested:

<%=if !session[:prebooking] || !session[:prebooking].end_date
link_to_remote day.date.mday, :url => {:action => ‘start_date’, :id =>
day}, :update => “months”
else
link_to day.date.mday, {:action => ‘check_rate’, :id => day}
end%>

Here’s what I came up with:

<%= link_to_remote @current_two_months[0][days_in_first_week].date.mday,
:url => {:action => ‘start_date’, :id => day}, :update => “month” if
@current_two_months[0][days_in_first_week].date.wday == i &&
session[:prebooking].end_date == nil%>

<%= link_to @current_two_months[0][days_in_first_week].date.mday,
{:action => ‘check_rate’, :id => day} if
@current_two_months[0][days_in_first_week].date.wday == i &&
session[:prebooking].end_date != nil%>

This way a XMLHttp post link is created if the user has not already
selected an end_date for their vacation stay. However, if the user has
already selected the end_date using javascript on the second calendar,
the application will move forward in the process using a standard get
link and vise versa. By the way, after updating the session data, with
either the start_date or end date, the link_to_remote method is set to
update the months partial so that the the AJAX links are replaced with
get requests.

Best Regards,

Sam W.

Sam W. wrote:

fails to render ‘check_rate’.
To do a normal render from a xhr request you have to use a RJS
redirect:

render :update do |page|
if session[:prebooking].start_date.nil?
# normal RJS
else
page.redirect_to :action => :check_rate
end
end


We develop, watch us RoR, in numbers too big to ignore.