1

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

 $('#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 } %>
0

3 Answers 3

3

This:

$('#Fav#{@fav_company.id}').hide();

Should be:

 $('#Fav #{@fav_company.id}').hide();

Assuming @fav_company.id presents a variable in the DOM

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. the id's of the divs are actually titled something like "Fav1232" but I played around with your suggestion and it didn't work. Perhaps I'm not passing the variable in right?
3

Since your javascript code is in an ERB file I think you should be using $('#Fav<%=@fav_company.id%>').hide();.

Comments

1

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

1 Comment

Then you need to post your views and all related stuff :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.