helpermethods - Executing ruby code from a view in rails -


this code executed view, , works:

<% @metatags = opengraph.fetch('http://www.someurl.com') || nil %> <% if @metatags != nil %>       posttitle = truncatestr('<%= @metatags.title %>', 72); <% end %> 

i'd same passing http://www.someurl.comas javascript-computed parameter, this

attempt 1

var someurl = ... (compute value); setcookie("someurl", someurl, 10000); <% @metatags = opengraph.fetch(cookies[:someurl]) || nil %> <% if @metatags != nil %>       posttitle = truncatestr('<%= @metatags.title %>', 72); <% end %> 

it doesn't work.

attempt 2

var someurl = ... (compute value); setcookie("someurl", someurl, 10000); <% readposturl %> <% if @metatags != nil %>       posttitle = truncatestr('<%= @metatags.title %>', 72); <% end %> 

and in controller

private def readposturl   @metatags = opengraph.fetch(cookies[:posturl]) || nil end helper_method :readposturl 

it doesn't work either.

both scenarios seem have troubles cookies. there way run opengraph.fetch(parameter) view javascript variable?

this common issue experienced developers new web development. since javascript runs in browser, executes (relatively) long time after ruby code has finished.

here's abridged version of http request/response cycle:

  1. browser makes request server
  2. server parses request
  3. server generates response (this ruby code running)
  4. browser receives response
  5. browser renders response (this javascript code running)

the solution typically pretty simple: make second request after javascript has run. have lot of options how make second request based on particular application (redirect, refresh, ajax).

as actual code, cookie approach if want keep information around future requests. if need once, stick data in url parameter second request.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -