js.erb is rendered as text
I have a link that needs to show a hidden Div on click.
For it, 开发者_JS百科I am using :
<%=link_to video.title,display_path(:format=>:js,:id=>video.id),:remote=>true%>
in the controller i have:
def display
@video=Video.find(params[:id])
respond_to do |format|
format.js
end
end
and in the js.erb, i have,
$('#vid_<%=@video.id%>').fadeIn('slow');
But when i run the app, upon clicking the link, the js.erb is rendered as text.It just displays:
$('#vid_<%=@video.id%>').fadeIn('slow');
You should read
Rails Ajax: .js.erb rendered as text, not JS
for a full explaination
You are doing everything right, but you can improve your code a little
views
# use spaces to ident all this stuff and write less if it is possible:
<%= link_to video.title, [:display, video], :remote => true %>
controller
class VideosController < ApplicationController
respond_to :html, :js
...
def display
@video=Video.find(params[:id])
respond_with @video
end
...
end
display.js.erb
$('#vid_<%= @video.id %>').fadeIn('slow');
Be sure, that you have installed jQuery:
rails g jquery:install
and included it into your application layout file.
<%= javascript_include_tag :defaults %>
Ok I found the solution.I made a silly mistake.I forgot to skip prototype while creating the App.This caused trouble when i tried to use Jquery.Its all good now.
精彩评论