Ajax使用路由检查数据库更改 [英] Ajax check for database change using route
问题描述
我正在使用具有延迟::: Job的回形针在后台处理照片。处理完成后,模型应将 photo.attachment_is_processing
设置为false。
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.
我需要检查此更改在客户端。我了解我可以通过使用Ajax查询路线来做到这一点,但是:
I need to check for this change on the client side. I understand I can do this by querying a route using Ajax, however:
路线应如何显示?
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
耙路的可能线索
:
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
完成此操作后,我该如何实际绑定照片.attachment_is_processing
到从该路由返回的是真还是假?
When that is done, how do I actually tie photo.attachment_is_processing
to the returning of true or false from that route?
推荐答案
您可能想要这样的东西:
You probably want something like this:
routes.rb
routes.rb
get '/check_photo_processing/:id', to: 'photos#check_photo_processing', as: :check_photo_processing
photos_controller。 rb
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
然后在视图/ photos / check_photo_processing.js
Then in views/photos/check_photo_processing.js
alert("<%= @photo_status %>");
// do what you gotta do with the status here
最后,要把所有的东西都绑在一起在一起,您需要一个函数来创建AJAX请求。如果您在应用中使用Coffeescript:
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"
)
然后从某处查看它
<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.
这篇关于Ajax使用路由检查数据库更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!