gpt4 book ai didi

javascript - Backbone /rails 之间使用 REST url 进行通信时出现问题

转载 作者:行者123 更新时间:2023-11-28 08:15:34 25 4
gpt4 key购买 nike

我正在开发 Rails x Backbone 应用程序。我有一个名为 Public_files 的 Rails Controller ,它允许我处理公共(public)文件夹上的文件。

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 上进行 GET

感谢您的帮助

最佳答案

您必须声明并设置模型的 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();

现在您将获得您想要的网址/public_files/ID

关于javascript - Backbone /rails 之间使用 REST url 进行通信时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23635353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com