How to use javascript variables in ruby

Hi friends,

How to use javascript variables in ruby.

i have activity id on a_id

function(a_id)
{
<%@foo=Activity.where("activity_id = ? ", a_id)
}

how can solve it

On Wed, Nov 13, 2013 at 7:30 AM, Daynthan K.
[email protected] wrote:

function(a_id)
{
<%@foo=Activity.where("activity_id = ? ", a_id)
}

Name it: app/assets/javascript/file.js.erb and it will go through ERB.

On Wed, Nov 13, 2013 at 7:55 AM, Jordon B. [email protected]
wrote:

On Wed, Nov 13, 2013 at 7:30 AM, Daynthan K. [email protected] wrote:

function(a_id)
{
<%@foo=Activity.where("activity_id = ? ", a_id)
}

Name it: app/assets/javascript/file.js.erb and it will go through ERB.

Actually I think I’m wrong try:
http://railscasts.com/episodes/324-passing-data-to-javascript?view=asciicast

On Nov 13, 2013, at 6:30 AM, Daynthan K. [email protected]
wrote:

how can solve it

It’s not at all clear to me what you are asking, but remember:

  • Ruby runs on the server;
  • Javascript runs in the browser.
  • Sometimes Ruby on the server is used to create/modify Javascript to be
    run in the browser.


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

On Nov 13, 2013, at 9:40 AM, Daynthan K. wrote:

alert("<%[email protected]%>");

If you’re going to use Erb in your JavaScript, then you must name your
file whatever.js.erb. Then your <%= %> will expand to be whatever the
local value of @user is at the moment that script is requested. You will
need a controller, too, and your request will need to have some context
around it. If your controller had a respond_to block in it, and your
JavaScript file was named sensibly for the controller method, then this
would Just Work,

respond_to do |format|
  format.js { render :layout => false }
  format.html
end

Walter

hi

here i have mention example code

On Nov 13, 2013, at 7:40 AM, Daynthan K. [email protected]
wrote:

here i have mention example code

OK, evaluate that code in light of what I said previously:

  • Ruby runs on the server;
  • Javascript runs in the browser.

Now:

  1. What do you think that code will do???
  2. What are you actually trying to accomplish?


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

On 13 November 2013 14:40, Daynthan K. [email protected]
wrote:

hi

here i have mention example code

var js_name="daya"; <%[email protected]=User.where("name=?", js_name%>

Remember the above is evaluated on the /server/ before the page is
rendered. js_name is not meaningful at that time.

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs