sending variable to a javascript file in RoR
I am using ajax to call a RoR rails function and am new to this.
The function is
def destroy
@fav_company = FavouriteCompany.find(params[:id])
respond_to do |format|
format.js { render :layout=>false }
end
end
In my destroy.js.erb I have开发者_StackOverflow中文版
$('#profile_alerts').show();
$('#Fav#{@fav_company.id}').hide();
The first line is working but not the second line. I am suspecting it is unable to access @fav_company.
What should I do? thanks
=====
Some additional information, the call I am making to this function is a link_to with remote => 'true' as such:
<%=link_to "destroy",{:controller=>"favourite_companies",:action=>"destroy", :id=>"#{fav.id}"}, {:remote => true } %>
This:
$('#Fav#{@fav_company.id}').hide();
Should be:
$('#Fav #{@fav_company.id}').hide();
Assuming @fav_company.id
presents a variable in the DOM
Since your javascript code is in an ERB file I think you should be using $('#Fav<%=@fav_company.id%>').hide();
.
Try to use:
$('#Fav#{escape_javascript({@fav_company.id}).html_safe}').hide();
==================================================================================
Ok, my code, but I does another task:
albums/show.html.haml
= link_to t('write.comment'), new_album_comment_path(@album, :format => :js, :id => @album.id), :remote => true
comments/new.js.haml
$("#comment_form").html("#{escape_javascript(render :partial => "comments/form").html_safe}");
also all code here https://github.com/barthezslavik/mebel - if you found something useful, I'll be happy
精彩评论