Rails 4 - 为什么我的控制器操作同时执行 html 和 json 响应,如日志中所示
Rails 4 - why my controller action executes both html and json response, as seen in the logs
我在日志中有一个 Rails 4 动作 no respond_to
block.But 我可以看到该动作呈现两次 - 一次对于 html 和其他对于 json.i 我想知道是什么导致了 this.Moreover 即使在发送了一个简单的字符串之后,id 属性仍然是 [object object]
- 像这样 - Parameters: {"id"=>"[object Object]"}
服务器日志。
###my route code
get 'halls/:id' => 'home#show_by_location', as: :show_by_location
####my homecontroller code
def show_by_location
if params[:id] and params[:id].present?
##get only halls address as addressable
@filter_results_found = false
if params[:capacity].present?
####some business logic added
@result = ##GETTING SOME RESULT HERE
if @result.size == 0
flash.now[:notice] = "No details found"
end
end
if params[:food_type].present?
###some business logic added
@result = ##GETTING SOME RESULT HERE
if @result.size == 0
flash.now[:notice] = "No halls found in #{@show_by_parameter.capitalize} that allow only #{food_type}"
end
end
if params[:rating].present?
@result = ##GETTING SOME RESULT HERE
flash.now[:notice] = "No details found in #{@show_by_parameter.capitalize} with rating of #{rating} star(s)" if @halls.size == 0
end
if params[:pricing].present?
@result = ##GETTING SOME RESULT HERE
end
end
if @filter_results_found === false
@result = ##GETTING SOME RESULT HERE
end
@result = @result.paginate(:page => params[:page])
end
####development/server log
Started GET "/halls/Sydney" for 43.242.228.212 at 2017-12-10 07:28:11 +0000
Cannot render console from 43.242.228.212! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by HomeController#show_by_location as HTML
Parameters: {"id"=>"Sydney"}
########other logs removed.....
.........
...again below is json rendering for same action
Started GET "/halls/[object%20Object]" for 43.242.228.212 at 2017-12-10 07:28:24 +0000
Cannot render console from 43.242.228.212! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by HomeController#show_by_location as JSON
Parameters: {"id"=>"[object Object]"}
我认为这不是一个好兆头,这会导致我的服务器处理我真正不需要的相同数据。
最终,我弄明白了 issue.I 是什么,我将其发布,以便它可以帮助面临类似问题的人。
问题
我在我的表单上使用 jquery fileupload plugin 将图像和视频上传到 go.Now 这个插件在脚本中有一个 ajax(json) 调用需要在页面上加载现有 images/videos。因此,如果脚本已加载,它总是调用 ajax 并且我在清单中添加了 js 文件 - application.js.So 每个请求都被调用 + json 从脚本调用。
解决方案
我必须删除 js 文件,然后将其显式包含在表单页面中 needed.This 还包括在 assets.rb
中添加一个新条目以单独加载脚本而不是资产管道。
Rails.application.config.assets.precompile += %w( video_upload image_upload )
并在所需模板中包含 js 文件 仅 -
<%= javascript_include_tag "jQuery-File-Upload-9.11.2/video_upload", :media => "all" %>
现在问题已解决,它在日志中也只有一个请求,而且表单正在使用 fileupload 插件
希望这对某人有所帮助。
我在日志中有一个 Rails 4 动作 no respond_to
block.But 我可以看到该动作呈现两次 - 一次对于 html 和其他对于 json.i 我想知道是什么导致了 this.Moreover 即使在发送了一个简单的字符串之后,id 属性仍然是 [object object]
- 像这样 - Parameters: {"id"=>"[object Object]"}
服务器日志。
###my route code
get 'halls/:id' => 'home#show_by_location', as: :show_by_location
####my homecontroller code
def show_by_location
if params[:id] and params[:id].present?
##get only halls address as addressable
@filter_results_found = false
if params[:capacity].present?
####some business logic added
@result = ##GETTING SOME RESULT HERE
if @result.size == 0
flash.now[:notice] = "No details found"
end
end
if params[:food_type].present?
###some business logic added
@result = ##GETTING SOME RESULT HERE
if @result.size == 0
flash.now[:notice] = "No halls found in #{@show_by_parameter.capitalize} that allow only #{food_type}"
end
end
if params[:rating].present?
@result = ##GETTING SOME RESULT HERE
flash.now[:notice] = "No details found in #{@show_by_parameter.capitalize} with rating of #{rating} star(s)" if @halls.size == 0
end
if params[:pricing].present?
@result = ##GETTING SOME RESULT HERE
end
end
if @filter_results_found === false
@result = ##GETTING SOME RESULT HERE
end
@result = @result.paginate(:page => params[:page])
end
####development/server log
Started GET "/halls/Sydney" for 43.242.228.212 at 2017-12-10 07:28:11 +0000
Cannot render console from 43.242.228.212! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by HomeController#show_by_location as HTML
Parameters: {"id"=>"Sydney"}
########other logs removed.....
.........
...again below is json rendering for same action
Started GET "/halls/[object%20Object]" for 43.242.228.212 at 2017-12-10 07:28:24 +0000
Cannot render console from 43.242.228.212! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by HomeController#show_by_location as JSON
Parameters: {"id"=>"[object Object]"}
我认为这不是一个好兆头,这会导致我的服务器处理我真正不需要的相同数据。
最终,我弄明白了 issue.I 是什么,我将其发布,以便它可以帮助面临类似问题的人。
问题
我在我的表单上使用 jquery fileupload plugin 将图像和视频上传到 go.Now 这个插件在脚本中有一个 ajax(json) 调用需要在页面上加载现有 images/videos。因此,如果脚本已加载,它总是调用 ajax 并且我在清单中添加了 js 文件 - application.js.So 每个请求都被调用 + json 从脚本调用。
解决方案
我必须删除 js 文件,然后将其显式包含在表单页面中 needed.This 还包括在 assets.rb
中添加一个新条目以单独加载脚本而不是资产管道。
Rails.application.config.assets.precompile += %w( video_upload image_upload )
并在所需模板中包含 js 文件 仅 -
<%= javascript_include_tag "jQuery-File-Upload-9.11.2/video_upload", :media => "all" %>
现在问题已解决,它在日志中也只有一个请求,而且表单正在使用 fileupload 插件
希望这对某人有所帮助。