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?
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.
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.
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.
<%= 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.
<%= 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.