-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.

Chinmay235
  • 3,236
  • 8
  • 62
  • 93

4 Answers4

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Can you please say me about the `module Siteconfiguration`? – Chinmay235 May 20 '16 at 07:20
  • I am getting error message `uninitialized constant ApplicationController::SiteConfiguration` – Chinmay235 May 20 '16 at 07:52
  • @Chinu You need to put `site_configuration.rb` somewhere that's auto-loaded. `app/concerns` is one place that's usually checked. – tadman May 20 '16 at 09:33
  • Ok. Let me know how to get site name in my layout? – Chinmay235 May 20 '16 at 10:03
  • It's just as you would with anything: `<%= SiteConfiguration.site_name %>`. – tadman May 20 '16 at 15:52
  • can you please briefly describe me. I am tried but unable to do that. – Chinmay235 May 31 '16 at 10:51
  • Here I am using using this `config/site_configuration.rb` I am putting this code `module SiteConfiguration mattr_accessor :site_name mattr_accessor :payment_provider end` and `config/environment.rb` putting `SiteConfiguration.site_name = 'The Site' SiteConfiguration.payment_provider = 'PayPal'` let me know is it right or not? – Chinmay235 May 31 '16 at 10:57
  • I am getting error message `uninitialized constant ActionView::CompiledTemplates::SiteConfiguration` – Chinmay235 May 31 '16 at 11:01
  • Finally i got my output. Thank you – Chinmay235 May 31 '16 at 11:30
  • I faced another problem `no implicit conversion of nil into String` – Chinmay235 Jun 01 '16 at 07:54
  • While I am getting `no implicit conversion of nil into String` message I restated my server then working fine. After some times I am getting same error. I mean each time I am restarting my server. – Chinmay235 Jun 01 '16 at 07:55
  • I have added one question. Please check http://stackoverflow.com/questions/37564295/how-to-fix-no-implicit-conversion-of-nil-into-string-in-ruby – Chinmay235 Jun 01 '16 at 09:28
0

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

ENV["host"] = "localhost:3000"
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"

0

Your application_controller.rb should be write below

$gallery_path = 'files/gallery/';

Your view file

<%=$gallery_path %>
Developer
  • 2,676
  • 8
  • 43
  • 65
  • This is extremely messy and should be an absolute last resort. Global variables are used very, very sparingly in Ruby. – tadman Apr 25 '16 at 09:03