How to use javascript variables in ruby

56 views
Skip to first unread message

Daynthan Kabilan

unread,
Nov 13, 2013, 8:30:15 AM11/13/13
to rubyonra...@googlegroups.com
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

Jordon Bedwell

unread,
Nov 13, 2013, 8:55:55 AM11/13/13
to rubyonra...@googlegroups.com
On Wed, Nov 13, 2013 at 7:30 AM, Daynthan Kabilan <dayan...@gmail.com> 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.

Jordon Bedwell

unread,
Nov 13, 2013, 8:58:26 AM11/13/13
to rubyonra...@googlegroups.com

Scott Ribe

unread,
Nov 13, 2013, 9:32:24 AM11/13/13
to rubyonra...@googlegroups.com
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 Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Daynthan Kabilan

unread,
Nov 13, 2013, 9:40:11 AM11/13/13
to rubyonra...@googlegroups.com
hi  

here i have mention example code

<script type="text/javascript">

var js_name="daya";

<%=@user=User.where("name=?", js_name%>

alert("<%=@user%>");

</script >

Walter Lee Davis

unread,
Nov 13, 2013, 9:45:23 AM11/13/13
to rubyonra...@googlegroups.com
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

>

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CADKeJyTqA%3DZ44f1RQeTtm5j8%3DpXQMessnu%3DEpm4WTMDGu33jbA%40mail.gmail.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Scott Ribe

unread,
Nov 13, 2013, 9:52:23 AM11/13/13
to rubyonra...@googlegroups.com, Daynthan Kabilan
On Nov 13, 2013, at 7:40 AM, Daynthan Kabilan <dayan...@gmail.com> 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?

Colin Law

unread,
Nov 13, 2013, 9:55:03 AM11/13/13
to rubyonra...@googlegroups.com
On 13 November 2013 14:40, Daynthan Kabilan <dayan...@gmail.com> wrote:
> hi
>
> here i have mention example code
>
> <script type="text/javascript">
>
> var js_name="daya";
>
> <%=@user=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
Reply all
Reply to author
Forward
0 new messages