-4

Now I am new in ruby language. Currently learning ruby. I am expert coder in the PHP. Basically I am working in the CakePHP. CakePHP and Ruby directory structure approximately same. But I am confusing little bit in the Ruby Code.

In CakePHP while I am define any variable in bootstrap.php I am getting this variable across the site.

Suppose I have a variable name $gallery_path = 'files/gallery/'; If I echo $gallery_path in controller, model and view. I am getting my gallery path.

But here in ruby I am unable to define any variable like php bootstrap.php Please suggest me.

6
  • you're looking for global variables... Commented Apr 25, 2016 at 7:26
  • @SuperPeanut No, global variables are not the answer here. Commented Apr 25, 2016 at 8:00
  • What sort of variables are you trying to define? Ruby strongly encourages people to organize these into modules or classes to simplify access and keep them from conflicting. Commented Apr 25, 2016 at 8:00
  • what Chinu is talking about in PHP is called global variable. So his question is : How can I use the global variable feature from PHP in Ruby... Commented Apr 25, 2016 at 8:51
  • Possible duplicate of Ruby on Rails: Where to define global constants? Commented Apr 25, 2016 at 10:35

4 Answers 4

2

For things that change frequently enough that you need to edit them now and then, but infrequently enough that you're not constantly changing it, a simple module works well here.

You can start with something like this:

module SiteConfiguration
  mattr_accessor :site_name
  mattr_accessor :payment_provider
end

Then you can configure that in config/environment.rb:

SiteConfiguration.site_name = 'The Site'
SiteConfiguration.payment_provider = 'PayPal'

These are accessible everywhere in your app.

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

12 Comments

Can you please say me about the module Siteconfiguration?
I am getting error message uninitialized constant ApplicationController::SiteConfiguration
@Chinu You need to put site_configuration.rb somewhere that's auto-loaded. app/concerns is one place that's usually checked.
Ok. Let me know how to get site name in my layout?
It's just as you would with anything: <%= SiteConfiguration.site_name %>.
|
0

you can use environment variables, try this gem: https://github.com/bkeepers/dotenv or you can define them:

ENV["host"] = "localhost:3000"

Comments

0

Your application_controller.rb should be write below

$gallery_path = 'files/gallery/';

Your view file

<%=$gallery_path %>

1 Comment

This is extremely messy and should be an absolute last resort. Global variables are used very, very sparingly in Ruby.
0

I think you mean global variables. In Ruby-on-Rails you can write it like

Controller:

@value = 2+1

HTML:

<%= @value %>

So your output in your browser will be "3"

Comments

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.