骨干网/rails之间使用REST url通信有问题
Trouble with communication between backbone/rails with a REST url
我正在开发Rails x Backbone应用程序。我有一个名为Public_files的rails控制器,它允许我在公共文件夹上的文件上工作。
class PublicFilesController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_public_file, only: [:show, :edit, :update, :destroy]
# GET /public_files/id
def show
path = params[:id]
if File.exists?( Rails.public_path.join( "#{path}.html" ) )
@content = File.read( Rails.public_path.join( "#{path}.html" ) )
render file: "public/#{path}", formats: [:html]
else
render file: 'public/404', status: 404, formats: [:html]
end
end
# GET /public_files/id/edit
def edit
path = params[:id]
if File.exists?( Rails.public_path.join( "#{path}.html" ) )
@content = File.read( Rails.public_path.join( "#{path}.html" ) )
render file: "public/#{path}", formats: [:html]
else
render file: 'public/404', status: 404, formats: [:html]
end
end
# POST /public_files
def create
path = params[:public_file]
content = params[:content]
if File.exists?( Rails.public_path.join( "#{path}.html" ) )
puts "The file you want to create already exist"
render file: 'public/404', status: 404, formats: [:html]
else
File.write(Rails.public_path.join( "#{path}.html" ), "#{content}")
render file: "public/#{path}", formats: [:html]
end
end
# PATCH/PUT /public_files/id
def update
path = params[:path]
content = params[:content]
if File.exists?( Rails.public_path.join( "#{path}.html" ) )
File.write(Rails.public_path.join( "#{path}.html" ), "#{content}")
render file: "public/#{path}", formats: [:html]
else
puts "The file you want to update doesn't exist"
render file: 'public/404', status: 404, formats: [:html]
end
end
# DELETE /public_files/id
# DELETE /public_files/id.json
def destroy
path = params[:id]
if File.exists?( Rails.public_path.join( "#{path}.html" ) )
File.delete( Rails.public_path.join( "#{path}.html" ) )
else
puts "The file you want to delete doesn't exist"
end
respond_to do |format|
format.html { redirect_to public_files_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_public_file
# @public_file = PublicFile.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def public_file_params
params[:public_file]
end
end
现在我想用Backbone在客户端处理文件。所以我写了一个Backbone模型。在我定义urlRoot的地方,我的模型将与rails的REST url通信。
class MyApp.Models.PulicFile extends Backbone.Model
urlRoot: '/public_files'
我只是想试试这是否有效。但我真的不知道如何在JS中实例化一个PublicFile对象,它的ID将是我到文件的路径。之后我可以创建一个my_file.fetch()
它会在/public_files/ID
谢谢你的帮助
您必须声明并设置模型的idAttribute
:
class MyApp.Models.PulicFile extends Backbone.Model
urlRoot: '/public_files'
idAttribute: 'yourId' // or the default value 'id'
然后在模型实例中,设置id并获取模型:
yourModel.set('yourId', theIdValue);
yourModel.fetch();
现在你将得到你想要的url /public_files/ID
相关文章:
- 如何使用 Backbone 读取通过同一 REST URL 返回的两个不同对象
- Kinvey URL REST GET 请求与相关表
- Dynamic domain URL Rest factory AngularJS
- 通过角度$HTTP将 URL 参数传递给 Rest 方法
- 尝试使用 SharePoint 2013 的 REST 服务器和 CSOM 时,“未为此应用程序的请求 URL 部署应用
- 在$.ajaxSetup()中设置REST服务调用的主url,并且仅在后续调用中设置服务方法
- dojo rest目标远程url
- 模型销毁调用了不正确的REST url
- 如何为我的所有BackboneJS模型/集合REST调用添加一个基本URL
- 来自外部URL的MeteorJS REST API(使用CollectionFS)
- 使用REST API上传文件(从公共url)到onedrive时出错
- EmberJS:自定义相关模型的REST URL
- 使用rest (jersey)时Url的最大长度
- 如何将自定义URL映射到SPA中的REST资源
- 主干模型-根据REST操作更改URL查询参数
- 自定义URL的REST API在Nodejs + mongodb
- 如何使用jquery从rest URL获取响应
- 骨干网/rails之间使用REST url通信有问题
- JMVC jQuery.Model - 不能覆盖标准的 REST URL
- 将REST url转换为javascript文字