Lectura asíncrona de la base de datos

I'm trying to create a modal window window containing detailed information.

# show.html.erb
<%= link_to 'Info', player_info_path, :remote => true %>

# Controller
def player_info
    @player = Player.find(1) # ignore the hard-coding

# player_info.js.erb
alert(<%= @player.name %> ); # alerts 'undefined'
$("#player_info").html("<%= escape_javascript(render("detailed_info"))%>");

The javascript is executed but how can i get the detailed info from the controller to the javascript or can I perform a CRUD-action from the js.erb?

preguntado el 03 de mayo de 12 a las 16:05

Puedes intentar alert("<%= @player.name %>"); (note the quotes!)? You should be able to access the player data using @player.attribute in your js view. -

Ah, of course! I really should have figured that out myself, but thanks for the answer! -

1 Respuestas

As noted in the comment, you were missing the quotes in alert("<%= @player.name %>");. You have everything else set up correctly - so you can access @player attributes in your player_info.js.erb like so: @player.attribute.

contestado el 03 de mayo de 12 a las 17:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.