1

I first apologize if my questions sounds fully, but I am completely new in Rails and I am not sure what is happening in the framework at all.

I am reading a book about "Ruby on Rails" and I need simply to remove some rows from table when button is clicked. The JavaScript I have added in the assets/javascript/*.js.coffee is as follows:

if $("#cart").length is 1
  $("#cart").hide "blind",
    direction: "vertical"
  , 1000
$("#cart tr").not(".total_line").remove()

The original JavaScript (since I am new to coffee js I have used a online converter to transform it and I am not sure if it is correct):

if ($('#cart').length == 1) { $('#cart').hide("blind", {direction: "vertical" }, 1000); }

$('#cart tr').not('.total_line').remove();

I have added in the destroy method of the controller:

format.js {}

and now the how method looks like:

  def destroy
    #@cart = Cart.find(params[:id])
    #@cart.destroy
    @cart = current_cart
    @cart.destroy
    session[:cart_id] = nil

    respond_to do |format|
      #format.html { redirect_to store_url,notice: 'Your cart is currently empty' }
      format.js { }
      format.html { redirect_to store_url }
      format.json { head :no_content }
    end
  end

but it gives me the following error (after button that should call the js is clicked):

Template is missing

Missing template carts/destroy, application/destroy with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/gotqn/Aptana Projects/depot/app/views"

After some search I found this question Template is missing in which it it said that I need to redirect to somewhere in my methods. I simply try writing:

format.js {head :no_content }

I an truly the error does not appear again, but the JavaScript is not executed.

I really need some help with this issue. Thanks in advance.

Community
  • 1
  • 1
gotqn
  • 42,737
  • 46
  • 157
  • 243

1 Answers1

2

Assets are not dynamic, they are generated on server start (at least on production). You need a view/carts/destroy.js.erb template containing the required code. If the js itself is static, then you can use the asset file to be executed in this template.

Matzi
  • 13,770
  • 4
  • 33
  • 50
  • But won't the JavaScript set in the assets folder be executed when the view is rendered? Or the JavaScript is rendered but the elements are not created and I should used on() function? – gotqn Nov 10 '12 at 10:30
  • With the missing template nothing is rendered. If you create the template, you can include the asset to be executed. Asset pipeline just generates a static js file, but it won't be included anywhere automatically, you need to do that. – Matzi Nov 10 '12 at 10:33
  • So, by template you mean view/carts/destroy.js.erb and in it I should put my JavaScript? – gotqn Nov 10 '12 at 10:40
  • And does 'format.js { }' in a controller's method looks for js.erb file with the same name, in the views folder? – gotqn Nov 10 '12 at 10:45
  • Yes, `format.js` looks for a template with the corresponding name. In that template you need to refresh the whole cart. As your script only hides an empty cart, so you need to clear is first, deleting the rows. After that, you can call the `hide` on the cart div. – Matzi Nov 10 '12 at 10:46
  • Thanks for your help. The things are a little bit clearer now. I have get the idea that in the views folder you should have only html.erb files and the all javascript files should be created in the assets folder. – gotqn Nov 10 '12 at 10:48