0

I added a configuration variable in application.rb such as

module Www
    class Application < Rails::Application
      ...
      config.myurl = "http://localhost:8443/play"
end

After something is clicked on my page I would like to use the value of this config var in order to come up with my full url such as mypage.js having

function ClickedOnSomething(someid){
    var fullUrl = <value of config.myurl> + '?id='+someid+'.html'; 
    ...
}

so that if for example the id of what I clicked on was 1776 then the fullUrl would be the string http://localhost:8443/play?id=1776.html

I am using Ruby on Rails 3.2.3 and Ruby 1.9.3

One thing I tried was going through the controller like the following:

In application_controller.rb I added

def before_filter
  # This variable will be available in all controller actions and views
  @url_global = Www::Application.config.myurl
end

In application.html.erb I added

<script type="text/javascript">
  var url_global = "<%= @url_global %>"
</script>

Then in mypage.js I did

function ClickedOnSomething(someid){
    var fullUrl = @url_global + '?id='+someid+'.html'; 
    ...
}

but @url_global is not recognized. I have actually tried many different variations of using the var in javascript but nothing is working for me.

0

1 Answer 1

1

I saw you declare

var url_global = "<%= @url_global %>";

so you have to use url_global in function, not @url_global;

var fullUrl = url_global + '?id='+someid+'.html'; 

Am I correct?

Sign up to request clarification or add additional context in comments.

3 Comments

I did try it that way without the @ and the variable wasn't being recognized either.
Can you open the rendered page by using view source in your browser? Send me the code it rendered
changing it to url_global without the @ did work after all. I just realized my problem was that in application_controller.rb I had before_filter twice. So I had class ApplicationController < ActionController::Base before_filter :set_cache_buster and then later on I had def before_filter # This variable will be available in all controller actions and views @url_global = Www::Application.config.myurl end So my guess is that the def was being skipped altogether However, I am curious if there is a better way to access config variables in javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.