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.com
as 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:
- browser makes request server
- server parses request
- server generates response (this ruby code running)
- browser receives response
- 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
Post a Comment