0.0
No commit activity in last 3 years
No release in over 3 years
export variable in rails to client-side
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

ClientVariable

exports your rails variables to client-side

Installation

Add this line to your application's Gemfile:

gem 'client_variable'

After you install and add it to your Gemfile, you need to run the generator:

rails g client_variable:install

It will create client_variable.yml in config folder, these variable in this file will be merged with variables defined through controller, exported to client-side

####client_variable.yml

development:
  app_name: <%= Rails.application.class.parent_name%>

production:
  app_name: <%= Rails.application.class.parent_name%>

Usage

Add this line to app/views/layouts/application.html.erb

<head>
  <title>some title</title>
  <%= include_variable %>
  <!-- include your action js code -->
  ...

Inspired from gon - https://github.com/gazay/gon, you can also do these in your controller

  1. Write variables by(change each request)
```ruby
client.variable_name = variable_value

client.push({
  :user_id => 1,
  :user_role => "admin"
})
```
  1. Or global You can use client.global for sending your data to js from anywhere! It's really great for some init data. ruby client.global.variable_name = variable_value

Add this line on top of app/assets/javascripts/application.js

//= require variable

I also define global variable in javascript rails Access the varaibles from your JavaScript file:

rails.get('variable_name') // => variable_value
rails.get('user_id') // => 1

rails.set('a.b.c.d', '123') // rails.values => {a: {b: {c: {d: '123'}}}}
rails.get('a.b.c.d') // => '123'

Options

:camel_case

include_variable(:camel_case => true) // => rails.values => {appName: 'Hello World'}
include_variable(:camel_case => false) // => rails.values => {app_name: 'Hello World'}