Fatin Cooper Fatin Cooper - 1 month ago 8
jQuery Question

Getting no response for rails Coffeescript "Alert" for a link_to

I am new to rails and coffee however, I simply attempting to get an alert when a link is clicked! What happens instead is nothing. It turns the link_to into a dead link

My Controller.rb

def index
@posts = Post.all
end

def new
@post = Post.new
respond_to do |format|
format.html
format.js
format.xml { render :xml => @post.to_xml }
end
end

def create
@post = Post.create!(params[:post])
format.html
format.js
redirect_to post_url
end


def post_params
params.require(:post).permit(:title,:body)
end


My index,html.erb looks like this

<%= link_to 'New', post_new_url, id: "go", :remote => true %>


and my post.coffee just has this code in it

$(document).on "page:change", ->
$("#go").click ->
alert "Clicked!"

Answer

I found out what was wrong. Apparent my code was bad.

$(document).on "click", "#go", ->
  alert "page has loaded!"