我遇到了下列CoffeeScript的麻煩:的CoffeeScript沒有完成,但沒有錯誤
jQuery ->
# Create a comment
$(".comment-form")
.on "ajax:beforeSend", (evt, xhr, settings) ->
$(this).find('textarea')
.addClass('uneditable-input')
.attr('disabled', 'disabled');
.on "ajax:success", (evt, data, status, xhr) ->
$(this).find('textarea')
.removeClass('uneditable-input')
.removeAttr('disabled', 'disabled')
.val('');
$(xhr.responseText).hide().insertAfter($(this)).show('slow')
# Delete a comment
$(document)
.on "ajax:beforeSend", ".comment", ->
$(this).fadeTo('fast', 0.5)
.on "ajax:success", ".comment", ->
$(this).hide('fast')
.on "ajax:error", ".comment", ->
$(this).fadeTo('fast', 1)
基本上,我有一個用戶的形式,我添加註釋。當添加新評論時,它應該改變類並禁用textarea,然後將數據發送到數據庫。然後它應該重置類,清除textarea並再次啓用textarea。最後,它應該在textarea之後添加新評論。
該代碼的第一部分工作,該類被添加到textarea,它被設置爲禁用,但腳本的其餘部分從未發生。當然,評論實際上保存在數據庫中,頁面的刷新將顯示評論。
我已經過了這麼多次,不知道出了什麼問題。我確實有一個earlier issue,而且腳本的縮進是錯誤的,但是已經修復。
我CommentController代碼如下:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :destroy]
def create
@comment_hash = comment_params
@obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
# Not implemented: check to see whether the user has permission to create a comment on this object
@comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
@comment.user = current_user
if @comment.save
render partial: "comments/comment", locals: { comment: @comment }, layout: false, status: :created
else
p @comment.errors
render js: "alert('error saving comment');"
end
end
def destroy
if @comment.destroy
render json: @comment, status: :ok
else
render js: "alert('error deleting comment');"
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:commentable_id, :commentable_type, :body, :user_id)
end
end
這裏是我創建的意見形式:
<div class='comment-form'>
<%= simple_form_for comment, remote: true do |f| %>
<%= f.input :body, input_html: { rows: "2" }, label: false %>
<%= f.input :commentable_id, as: :hidden, value: comment.commentable_id %>
<%= f.input :commentable_type, as: :hidden, value: comment.commentable_type %>
<%= f.button :submit, 'Save Note', class: "button tiny radius", disable_with: "Submitting…" %>
<% end %>
</div>
這是我的顯示留言代碼:
<div class='comment'>
<hr>
<%=link_to "×", comment_path(comment), method: :delete, remote: true, data: { confirm: 'Are you sure you want to remove this comment?',disable_with: 'x' }, class: 'close' %>
<small><%=comment.updated_at.to_s(:short) %></small>
<p><%= comment.body %></p>
順便說一句,刪除操作部分起作用。它從數據庫中刪除評論,並隱藏所有評論。頁面刷新顯示尚未刪除的評論。
任何幫助將不勝感激,因爲我不知道該怎麼去與這一點。在我看來,它應該起作用,所以我必須錯過簡單的東西。
爲什麼在咖啡代碼中使用分號?在最新的Coffeescript中,你可以編寫你的代碼更習慣https://gist.github.com/elclanrs/077b1271014d1a039afb。 – elclanrs
另外,我認爲你的身份可能有問題,那麼'.on「ajax:success」'綁定到什麼?看到我編輯的要點... – elclanrs
是的,就是這樣。完全錯過了縮進。感謝您幫助這款咖啡小白菜。 –