问题
I'm using Paperclip with Delayed::Job to process photos in the background. When processing is done, the model should set photo.attachment_is_processing
to false.
I need to check for this change on the client side. I understand I can do this by querying a route using Ajax, however:
How should the route look like?
Possible clues from schema.rb
:
create_table "forem_posts", force: :cascade do |t|
t.integer "topic_id"
t.text "text"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "reply_to_id"
t.string "state", default: "pending_review"
t.boolean "notified", default: false
end
create_table "photos", force: :cascade do |t|
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "attachment_file_name"
t.string "attachment_content_type"
t.integer "attachment_file_size"
t.datetime "attachment_updated_at"
t.boolean "attachment_is_animated"
t.boolean "attachment_is_processing", default: false
end
Possible clues from rake routes
:
forum_topic_posts POST /:forum_id/topics/:topic_id/posts(.:format) forem/posts#create
new_forum_topic_post GET /:forum_id/topics/:topic_id/posts/new(.:format) forem/posts#new
edit_forum_topic_post GET /:forum_id/topics/:topic_id/posts/:id/edit(.:format) forem/posts#edit
forum_topic_post GET /:forum_id/topics/:topic_id/posts/:id(.:format) forem/posts#show
PATCH /:forum_id/topics/:topic_id/posts/:id(.:format) forem/posts#update
PUT /:forum_id/topics/:topic_id/posts/:id(.:format) forem/posts#update
When that is done, how do I actually tie photo.attachment_is_processing
to the returning of true or false from that route?
回答1:
You probably want something like this:
routes.rb
get '/check_photo_processing/:id', to: 'photos#check_photo_processing', as: :check_photo_processing
photos_controller.rb
def check_photo_processing
@photo_status = Photo.find(params[:photo_id]).attachment_is_processing
respond_to do |wants|
wants.js
end
end
Then in views/photos/check_photo_processing.js
alert("<%= @photo_status %>");
// do what you gotta do with the status here
Finally, to tie it all together you need a function to create the AJAX request. If you're using Coffeescript in your app:
check_photo_status: ( photo_id ) ->
$.ajax(
type: 'GET'
url: "/check_photo_status/#{ photo_id }.js"
)
Then call it from a view somewhere
<script>
check_photo_status(@photo.id);
// or pass in the photo id somehow
</script>
Because there wasn't much sample code provided in the question, this is only a basic configuration for how to accomplish what you need. You'll probably have to tweak it a bit to get it working with your app.
来源:https://stackoverflow.com/questions/29329620/ajax-check-for-database-change-using-route