Question : how can I toggle a variable true/false with a button(or text link) on the page?

I need to toggle a variable (high resolution/low resolution) on my video display pages.  I need the visitor to be able to click a button/link to trigger a method that toggles a variable, then re-renders the page it came from.

That way the variable being either true or false will dictate the video displayed via an if statement.

the variable would be 'hi_res'

Thanks in advance!

Answer : how can I toggle a variable true/false with a button(or text link) on the page?

But, you don't have anywhere in toggle_resolution that says:
@video = Video.find(....)

So, @video is nil and will throw an error when you try to access it from the partial.
It should say something like "tried to call function youtube_link() on nil variable" in the log.
Those rjs errors don't get sent back to the browser, so you have to watch for them.

Try the below code and let me know.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
# You currently have:
<%= link_to_remote "Switch resolution",
        :url => {:action => 'toggle_resolution'} %>
 
# Instead, include the id as well so you can look up the video to be refreshed
<%= link_to_remote "Switch resolution",
        :url => {:action => 'toggle_resolution', :id = @video.id} %>
 
# Update your toggle_resolution action as so:
# do a find() for the @video and don't bother passing it as a 
# local because partials can see instance variables
def toggle_resolution 
  @video = Video.find(params[:id])
  session[:hi_res] = !session[:hi_res]  # toggle true/false
  @res_val = session[:hi_res] 
  render :update do |page|
    page.replace_html 'video_display', :partial => 'video_display'
  end
end
Open in New Window Select All
Random Solutions  
 
programming4us programming4us