Passing instance variable to js.erb

Rails 3.1.3

Hi again.

I would like to pass an instance variable to “HERE!!!” in ‘save.js.erb’

$(’#listtrans’).html(
'<%= escape_javascript(render :partial => “videos/script_list”,
:locals => { :scripts => Script.find_all_by_video_id(HERE!!!) } ) %>
');

If I put a specific value, video_id, (18 in this case ) like

$(’#listtrans’).html(’<%= escape_javascript(render :partial =>
“videos/script_list”, :locals => { :scripts =>
Script.find_all_by_video_id(18) } ) %>’);

works fine. Of course, this must be generalized.

So I tried,

$(’#listtrans’).html(’<%= escape_javascript(render :partial =>
“videos/script_list”, :locals => { :scripts =>
Script.find_all_by_video_id(<%= @video.id %>) } ) %>’);

gives an error

ActionView::Template::Error
(/Users/ishidasouichi/Dropbox/Rails/script_youtube/app/views/videos/save.js.erb:3:
syntax error, unexpected ‘<’, expecting ‘)’
…> Script.find_all_by_video_id(<%= @video.id );@output_buffer

Also, I tried

@video.id
params[:video_id]
and so on.

Any idea?

Thanks in advance?

soichi

On 16 February 2012 05:09, Soichi I. [email protected] wrote:

I would like to pass an instance variable to “HERE!!!” in ‘save.js.erb’

$(‘#listtrans’).html(‘<%= escape_javascript(render :partial =>
“videos/script_list”, :locals => { :scripts =>
Script.find_all_by_video_id(@video.id) } ) %>’)

Thanks for your answer but it give the error

Started POST “/scripts” for 127.0.0.1 at 2012-02-16 15:53:50 +0900
Processing by ScriptsController#create as JS
Parameters: {“utf8”=>“✓”,
“authenticity_token”=>“9rMiPwxlQrXiAIgUy8Qqe77KXDezXmpLF4WLupZb5ME=”,
“script”=>{“video_id”=>“18”, “startp”=>“85”, “text”=>“korejyadamedann”},
“commit”=>“save”}
(0.1ms) SELECT 1 FROM “scripts” WHERE “scripts”.“startp” = 85 LIMIT
1
SQL (0.7ms) INSERT INTO “scripts” (“created_at”, “startp”, “text”,
“updated_at”, “video_id”) VALUES (?, ?, ?, ?, ?) [[“created_at”, Thu,
16 Feb 2012 06:53:50 UTC +00:00], [“startp”, 85], [“text”,
“korejyadamedann”], [“updated_at”, Thu, 16 Feb 2012 06:53:50 UTC
+00:00], [“video_id”, 18]]
Rendered videos/save.js.erb (1.5ms)
Completed 500 Internal Server Error in 138ms

ActionView::Template::Error (Called id for nil, which would mistakenly
be 4 – if you really wanted the id of nil, use object_id):
1:
2:
3: $(’#listtrans’).html(’<%= escape_javascript(render :partial =>
“videos/script_list”, :locals => { :scripts =>
Script.find_all_by_video_id(@video.id) } ) %>’);
4:
5:
6:
app/views/videos/save.js.erb:3:in
_app_views_videos_save_js_erb__2673251445733535399_2157206780' app/controllers/scripts_controller.rb:47:inblock (2 levels) in
create’
app/controllers/scripts_controller.rb:43:in `create’

Does anyone know why?

soichi

Thanks. To be honest, I was having the ‘installing debugger’ problem as
well :wink:
It seems many people have the same problem in installing ruby-debug19.

So I hoped someone can guess from my writing but I realized the
importance of debugger now.

Thanks anyway. I will come back when the debugger problem is resolved.

sochi

On 16 February 2012 06:55, Soichi I. [email protected] wrote:

Thanks for your answer but it give the error

Did you read the error? Google it?

ActionView::Template::Error (Called id for nil, which would mistakenly
be 4 – if you really wanted the id of nil, use object_id):

3: $(‘#listtrans’).html(‘<%= escape_javascript(render :partial =>
“videos/script_list”, :locals => { :scripts =>
Script.find_all_by_video_id(@video.id) } ) %>’);

app/views/videos/save.js.erb:3:in

Does anyone know why?

It’s most likely that @video (the instance variable you want the id
of) is nil. Use debugging techniques to break into the code where it’s
being set and being used to inspect it.

On Thu, Feb 16, 2012 at 10:49 AM, Soichi I.
[email protected]wrote:

Thanks. To be honest, I was having the ‘installing debugger’ problem as
well :wink:
It seems many people have the same problem in installing ruby-debug19.

So I hoped someone can guess from my writing but I realized the
importance of debugger now.

Thanks anyway. I will come back when the debugger problem is resolved.

For getting ruby debugger to work in 1.9.3 with Rails Gemfile, I got it
working,
but was quite some sweat …

http://rails.vandenabeele.com/blog/2011/12/21/installing-ruby-debug19-with-ruby-1-dot-9-3-on-rvm/

I just checked now, and it see the latest version of linecache19 is
still
0.5.12
(and the debugging only works with linecache 0.5.13). Probably a similar
problem with ruby-debug-base19 being 0.11.25 but needs to be 0.11.26 .

HTH,

Peter

Thanks. Finally ruby-debug19 is working for me. It took me a long time
:wink:

soichi

On 16 February 2012 09:49, Soichi I. [email protected] wrote:

Thanks. To be honest, I was having the ‘installing debugger’ problem as
well :wink:

You can debug using “puts”… it’s can just be painful and slow to
resolve issues (although you could’ve sorted this problem with it
quite fast :wink:

On Feb 15, 11:09pm, Soichi I. [email protected] wrote:

If I put a specific value, video_id, (18 in this case ) like

Check out the gem ‘gon’ along with Ryan B.’ recent RailsCast on the
topic of passing variables to JavaScript.

Thanks. It will help!

soichi

you should not use js with erb, it meet serious problem when you change
variable rails without change js. You can use client variable gem,
checkout
more details GitHub - Coffa/client_variable: Using rails variable in javascript

Not as old a message as I’ve responded to, but this one was from
February of 2012