Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, CoffeeScript and ERB

So I'm messing around witha few javascript responders for a few actions. So, for instance,

def create
  #code omitted
  respond_to do |format|
    if @post.save
      format.html { redirect_to discussion_posts_path(@post.discussion), notice: 'Post was successfully created.' }
      format.js
    else
      format.html { render :action => "new" }
    end
  end
end

#create.js.erb
$("#discussion_posts_table").append("<%= escape_javascript(render(@post)) %>");
$("#post_body").val("");

Now lets say I wanted to convert these javascript functions to Coffeescript (setting aside the fact that it gains almost nothing), if I change the file to create.js.coffee.erb it stops working. What's the proper way to use coffeescript in UJS?

For the record, I have coffee-rails in my Gemfile, and am using coffeescript in the asset pipeline.

like image 947
DVG Avatar asked Jun 21 '26 08:06

DVG


1 Answers

I've seen .coffee.erb used before. Still looking around for that project.

Ok if they're still in your views, just use .js.coffee

Edit 1: Apparently Rails will still process the erb oddly enough. Edit 2: Also, you can add erb to the end of a js.coffee file, they're jsut preprocessed in a particular order thus that might have thrown off Rails.

like image 169
TheIrishGuy Avatar answered Jun 23 '26 01:06

TheIrishGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!